Friday, 17 December 2010

Filtering the URL in text, checking the links/URLs in text

/ *
* @param string $text = the text for checking URLs/Links.
* @return array $urls = 'is_url_exist' for true/false, 'urls'= list of found URLs
* /
static function is_url_exist_in_text($text="")
{
$urls = array();
$is_url_exist = false;

/ / $reg_exUrl = "/((http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)|(/[a-zA-Z\.]+\.[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/))?/";
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // for url format : http://xxx.com/xxx | http://xxx.com
preg_match($reg_exUrl, $text, $url_1);
$reg_exUrl = "/(www)\.[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; // for url format : www.xxx.com | http://www.xxx.com
preg_match($reg_exUrl, $text, $url_2);

$urls = array_merge($url_1, $url_2);
if(count($urls)>=1) { $is_url_exist = true; }

$results = array('is_url_exist'=>$is_url_exist, 'urls'=>$urls);

return $results;
}

No comments:

Post a Comment

Please post any queries and comments here.