PHP实现对站点内容外部链接的过滤方法
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了PHP实现对站点内容外部链接的过滤方法,可实现针对外部链接增加rel="nofollow"的功能,有助于网站SEO建设以及避免不必要的损失,是非常实用的技巧,需要的朋友可以参考下
熟悉SEO的朋友都知道,对于网站外部链接失效的情况如果链接带有rel="nofollow"属性可以避免不必要的损失。本文就以实例形式演示了PHP实现对站点内容外部链接的过滤方法。具体如下:
问题描述:原来站内很多文章都是摘录的外部文章,文章里很多链接要么是时间久了失效了,要么就是一些测试的网址,如: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属性
注意!这里过滤的域名需要是不带“/”的,如https://.jb51.,这样才可以很好的过滤。
相信本文所述的方法对大家的PHP项目开发有一定的借鉴价值。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程