php regex operation for url and # value detection -


this question has answer here:

i want make url occurrence in text string link. when 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 @ places.

also following regex expression not able detect url ar starting www.

$reg_exurl1 = "/(http|https|ftp|ftps)\:\/\/[a-za-z0-9\-\.]+\.[a-za-z]{2,3}(\/\s*)?/";  $text = "the text want filter goes here. http://www.google.com data in 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 text return text  }else {     echo "in else #$".$text;  } 

this code # value detection in string.

$text= "this detection of #tag values in text. #special values #hash shown 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

$reg_exurl1 = "~(http|https|ftp|ftps)://[a-za-z0-9-.]+\.[a-za-z]{2,3}(/s*)?~";  $text = "the text want filter goes here. http://www.google.com data in 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 text return 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; 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -