I want to make all the URL occurrence in text string as link. When I tried
preg_match($reg_exUrl, $text, $url)
echo preg_replace($reg_exUrl1, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
it has replaced first match URL at all the places.
Also following regex expression was not able to detect URL which ar starting with www.
$reg_exUrl1 = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "The text you want to filter goes here. http://www.google.com data which in the http://www.apple.com";
if( preg_match($reg_exUrl, $text, $url)){
echo preg_replace($reg_exUrl1, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
// if no URLs in the text just return the text
}else {
echo "IN Else #$".$text;
}
This is the code for # value detection in string.
$text= "This is a detection of a #tag values in the text. any #special values with #hash will be shown as link";
$reg_exUrl ="/#\w+/";
if( preg_match($reg_exUrl1, $text, $url)){
echo preg_replace($reg_exUrl1, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
}else {
echo "not Matched".$text;
}
Check this
$reg_exUrl1 = "~(http|https|ftp|ftps)://[a-zA-Z0-9-.]+\.[a-zA-Z]{2,3}(/S*)?~";
$text = "The text you want to filter goes here. http://www.google.com data which in the http://www.apple.com";
if( preg_match($reg_exUrl1, $text, $url)){
echo preg_replace($reg_exUrl1, '<a href="$0" rel="nofollow">$0</a>', $text);
// if no URLs in the text just return the text
}else {
echo "IN Else #$".$text;
}
UPDATE
$reg_exUrl ="/#\w+/";
if( preg_match($reg_exUrl, $text, $url)){
echo preg_replace($reg_exUrl, '<a href="$0" rel="nofollow">$0</a>', $text);
}else {
echo "not Matched".$text;