php regex operation for url and # value detection -
this question has answer here:
- extract urls text in php 11 answers
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
Post a Comment