一般在做网站系统的时候,出于优化等因素的考虑需要再添加文章的时候删除掉不是本站的链接,对于这一要求可以通过让PHP处理下文章内容,来达到文章外部链接的自动删除的效果。
本实例代码主要参考织梦CMS内容管理系统的外链删除方法。
复制代码 代码如下:
/**
* 删除非站内链接
*
* @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;
}
http://www.bkjia.com/PHPjc/825360.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825360.htmlTechArticle一般在做网站系统的时候,出于优化等因素的考虑需要再添加文章的时候删除掉不是本站的链接,对于这一要求可以通过让PHP处理下文章内...
Stellungnahme:Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn