PHP实现删除非站内外部链接实例代码
2021-06-27 03:04
标签:ace 超链接 url amp continue cti empty 实现 function /** PHP实现删除非站内外部链接实例代码 标签:ace 超链接 url amp continue cti empty 实现 function 原文地址:http://www.cnblogs.com/ZDPPU/p/7149231.html
* 删除非站内链接
*
* @access public
* @param string $body 内容
* @param array $allow_urls 允许的超链接
* @return string
*/
function Replace_Links( &$body, $allow_urls=array() )
{
$host_rule = join(‘|‘, $allow_urls);
$host_rule = preg_replace("#[\n\r]#", ‘‘, $host_rule);
$host_rule = str_replace(‘.‘, "\\.", $host_rule);
$host_rule = str_replace(‘/‘, "\\/", $host_rule);
$arr = ‘‘;
preg_match_all("#]*)>(.*)#iU", $body, $arr);
if( is_array($arr[0]) )
{
$rparr = array();
$tgarr = array();
foreach($arr[0] as $i=>$v)
{
if( $host_rule != ‘‘ && preg_match(‘#‘.$host_rule.‘#i‘, $arr[1][$i]) )
{
continue;
} else {
$rparr[] = $v;
$tgarr[] = $arr[2][$i];
}
}
if( !empty($rparr) )
{
$body = str_replace($rparr, $tgarr, $body);
}
}
$arr = $rparr = $tgarr = ‘‘;
return $body;
}