Home  >  Article  >  Backend Development  >  php正则过滤超链接并判断链接文字是否为网址preg_replace_callback函数用法

php正则过滤超链接并判断链接文字是否为网址preg_replace_callback函数用法

WBOY
WBOYOriginal
2016-06-20 13:01:001095browse

php正则过滤超链接并判断链接文字是否为网址preg_replace_callback函数用法

$str = '<a class="style" href="http://www.scutephp.com/" target="_blank">www.scutephp.com</a> <a href="http://www.scutephp.com">cxybl</a> 过滤超链接';
$str = filter_url($str);


函数代码如下:

function filter_url($str){
return preg_replace_callback("/<a[^>]+>(.+?)<\/a>/i","filter_url_callback",$str);
}
function filter_url_callback($matchs){
$str = $matchs[1];
if(!$str) return '';
$arr = array('www.','http://','.com','.cn','.org','.net','.cc');
foreach($arr AS $k=>$v){
if(stripos($str,$v) !==false) return '';
}
return $str;
}


如果超链接的文字为 www.scutephp.com 的网址,也会被过滤掉.但是如果是纯文字的话就保留.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn