-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; 

}

3
  • Instead of posting new questions why don't you search first ? stackoverflow.com/questions/910912/… Commented Sep 30, 2015 at 6: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. Commented Sep 30, 2015 at 6: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. Commented Sep 30, 2015 at 7:00

1 Answer 1

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; 
Sign up to request clarification or add additional context in comments.

8 Comments

not working. also it is replacing first match url to every where.
@Mam Programmer, it worked.
I need one more favour can you tell me same for the # value detection. and replace all #value values with a link
can you paste any sample code?
sure, just a second i will add it into question.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.