A5下载 - 努力做内容最丰富最安全的下载站!

A5站长下载站

当前位置:A5下载 > 源码技巧 > 父类数据 > PHP实现对站点内容外部链接的过滤方法

PHP实现对站点内容外部链接的过滤方法

时间:2015-06-04 17:43作者:sa人气:28

熟悉SEO的朋友都知道,对于网站外部链接失效的情况如果链接带有rel="nofollow"属性可以避免不必要的损失。本文就以实例形式演示了PHP实现对站点内容外部链接的过滤方法。具体如下:

问题描述:原来站内很多文章都是摘录的外部文章,文章里很多链接要么是时间久了失效了,要么就是一些测试的网址,如:http://localhost/ 之类的,链接多了的话,就形成站内很多死链接,这对SEO优化是很不利的。

解决方法:需要对站点内的内容进行过滤,将不是内部链接的链接加上 rel="nofollow"属性。

本文借鉴了wordpress的过滤外部链接的函数,将其改一下即可使用。

function content_nofollow($content,$domain){

preg_match_all('/href="(.*?)"/',$content,$matches);

if($matches){

foreach($matches[1] as $val){

if( strpos($val,$domain)===false )

$content=str_replace('href="'.$val.'"', 'href="'.$val.'" rel="external nofollow" ',$content);

}

}

preg_match_all('/src="(.*?)"/',$content,$matches);

if($matches){

foreach($matches[1] as $val){

if( strpos($val,$domain)===false )

$content=str_replace('src="'.$val.'"', 'src="'.$val.'" rel="external nofollow" ',$content);

}

}

return $content; }

 

标签PHP,实现,站点,内容,外部,链接,过滤,方法,熟悉,SE

相关下载

查看所有评论+

网友评论

网友
您的评论需要经过审核才能显示

公众号