php url 和 # 值检测的正则表达式操作
php regex operation for url and # value detection
我想将文本字符串中出现的所有 URL 作为 link。
当我尝试
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;
}
这是字符串中#值检测的代码。
$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;
}
检查这个
$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="[=10=]" rel="nofollow">[=10=]</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="[=11=]" rel="nofollow">[=11=]</a>', $text);
}else {
echo "not Matched".$text;
我想将文本字符串中出现的所有 URL 作为 link。 当我尝试
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;
}
这是字符串中#值检测的代码。
$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;
}
检查这个
$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="[=10=]" rel="nofollow">[=10=]</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="[=11=]" rel="nofollow">[=11=]</a>', $text);
}else {
echo "not Matched".$text;