用于url和#值检测的PHP正则表达式操作


php regex operation for url and # value detection

我想让所有的URL出现在文本字符串作为链接。当我尝试

preg_match($reg_exUrl, $text, $url) echo preg_replace($reg_exUrl1, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);

它已经替换了所有地方的第一个匹配URL。

后面的正则表达式也无法检测以www开头的URL。

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

这是字符串中# value检测的代码。

$text= "这是对文本中#tag值的检测。任何带有#hash的#特殊值将显示为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;

}

检查

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

更新
$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;