PHP培训常见问题
选课中心 APP下载
当前位置:首页 > 技能类 > PHP培训 > 常见问题 > php如何去除外链

php如何去除外链

更新时间:2020-09-02 02:51:46 来源: 阅读量:

【摘要】 PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。下面是php如何去除外链,让我们一起来看看php如何去除外链的具体内容吧!

php如何去除外链

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如何去除外链的内容,对PHP感兴趣的小伙伴们可以关注考必过,获取更多PHP前沿资讯和最新技术。最新消息小编会第一时间发布,助力大家考试,加油!

分享到: 编辑:weiwei

常见问题关键词