-1

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;

}

Mitul Maheshwari
  • 2,647
  • 4
  • 24
  • 38
  • Instead of posting new questions why don't you search first ? http://stackoverflow.com/questions/910912/extract-urls-from-text-in-php – Pedro Lobito Sep 30 '15 at 06:31
  • @PedroLobito, I have already googled it and found the link which you have posted , but if you try any of those given solution, it is not giving desired output. That's the reason i raise the question again. – Mitul Maheshwari Sep 30 '15 at 06:46
  • @PedroLobito if you check care fully my quetion if little bit diffrent than what you have suggested. My problem was i am not able to replace all url, one url detect will work fine in the code which i have pested. – Mitul Maheshwari Sep 30 '15 at 07:00

1 Answers1

1

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;
Man Programmer
  • 5,300
  • 2
  • 21
  • 21