我想将文本字符串中的所有URL出现为链接。 我试过的时候
preg_match($reg_exUrl, $text, $url)
echo preg_replace($reg_exUrl1, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);
它已取代所有地方的第一个匹配网址。
此外,正则表达式无法检测以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;
}
这是字符串中#值检测的代码。
$ text =“这是对文本中#tag值的检测。任何带#hash的#special值都将显示为链接”;
$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;
}
答案 0 :(得分:1)
检查
$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;