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

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

WBOY
Release: 2016-06-20 13:01:00
Original
1133 people have browsed it

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);
Copy after login


函数代码如下:

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;
}
Copy after login


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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template