×
新网 > 建站推广 > 正文

php如何去除外链

  • 作者:zccc
  • 来源:网络
  • 2020-09-01 15:04:47

php去除外链的方法:将不是内部链接的链接加上【rel="nofollow"】属性,代码为【$a['content'] = content_nofollow($a['content'],$domain);】。【相关学习推

php去除外链的方法:将不是内部链接的链接加上【rel="nofollow"】属性,代码为【$a['content'] = content_nofollow($a['content'],$domain);】。

【相关学习推荐:php图文教程】

php去除外链的方法:

解决方法:需要对站点内的内容进行过滤,将不是内部链接的链接加上 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属性

相关学习推荐:php编程(视频)

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading