PHP过滤外部链接及外部图片 添加rel="nofollow"属性

王朝学院·作者佚名  2016-05-25
窄屏简体版  字體: 小  |  中  |  大  |  超大  

php过滤外部链接及外部图片 添加rel="nofollow"属性原来站内很多文章都是摘录的外部文章,文章里很多链接要么是时间久了失效了,要么就是一些测试的网址,如:http://localhost/ 之类的,链接多了的话,就形成站内很多死链接,这对SEO优化是很不利的。那么就需要对站点内的内容进行过滤,将不是内部链接的链接加上 rel="nofollow"属性。

网上找到了WordPRess的过滤外部链接的函数,将其改一下即可使用

//外部链接增加nofllow $content 内容 $domain 当前网站域名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;}调用的时候很好调用,如下是调用演示$a['content'] = content_nofollow($a['content'],$domain); //将文章内容里的链接增加nofllow属性注意!过滤的域名需要是不带“/”的,如http://www.ledaokj.com这样才可以很好的过滤。原文链接:增加对站点内容外部链接的过滤

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航