PHP过滤外部链接及外部图片 添加rel="nofollow"属性
2014-05-05 10:41
483 查看
原来站内很多文章都是摘录的外部文章,文章里很多链接要么是时间久了失效了,要么就是一些测试的网址,如: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
这样才可以很好的过滤。
原文链接:增加对站点内容外部链接的过滤
网上找到了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
这样才可以很好的过滤。
原文链接:增加对站点内容外部链接的过滤
相关文章推荐
- 为 WordPress 标签添加 rel="nofollow" 属性
- a标签的rel="external nofollow"属性
- "rel=nofollow"属性简介
- "rel=nofollow"属性--SEO基础
- "rel=nofollow"属性
- rel=nofollow"属性 与SEO的关系-----SEO基础
- "rel=nofollow"属性简介
- python提取CSS文件中背景图片等属性的外部链接值
- 链接外部样式表标签link中的rel/rev属性
- 关于rel="nofollow"
- ci框架分页类如何添加rel=nofollow
- 如何在linux的gcc中添加c语言的外部链接库(“比如说,math.h"
- php可以在类的定义外部添加属性
- html rel="external nofollow"
- 如何让WordPress自动对外链添加"nofollow"标签
- 超链接 target="_blank" 要增加 rel="nofollow me noopener noreferrer" 来堵住钓鱼安全漏洞
- PHP正则分步过滤文章中的超链和文字链接,同时保留包含有图片的超链、远程引入的js脚本、独立的图片等链接
- linux php添加mysql扩展时报错: No such file or directory #include "ext/mysqlnd.h"
- a标签的下载链接,如果是图片,点击链接是下载一个图片而不是直接打开,应该怎么办?(添加download属性)
- Bigcommerce产品分类:给重复的CategoryName链接添加rel="nofllow"标记