正务库

EMLOG文章外链自动本地化或直接外链加nofollow属性的方法

时间:2年前   游览量:319    标签: nbsp    39    quot    replace    代码   

    做网站的都或多或少动那么一点 SEO的知识,在发文章的时候对于站外链接都想加上一个nofollow的属性,以防止本站的权重不流失,手动给每个链接增加nofollow有些费事,于是乎就想到了用PHP代码自动把非本站网址的链接加上nofollow属性,今天分享的这个PHP代码就是这个用处,你只需要把下面代码复制粘贴到指定位置后,你就不用担心每次都手动给文章内容里的站外链接添加nofollow属性了;链接添加nofollow属性不易过多,适当才好,这个还是站长自己把握吧,有需要的就从下方把代码复制去吧。
方法一(外链加nofollow属性):将下面代码复制到module.php文件里去

<?php //文章外链本地化
function sl_wl($log_content,$domain){
preg_match_all('/hr ef="(.*?)"/', $log_content, $matches);
if($matches){foreach($matches[1] as $val){
if(strpos(str_replace(array('http://','https://'),'',$val),str_replace(array('http://','https://'),'',$domain)) === false){
$log_content = str_replace('hr ef="'.$val.'"','hr ef="'.$val.'" rel="nofollow"',$log_content);}}}
preg_match_all('/src="(.*?)"/', $log_content, $matches);
if($matches){
foreach($matches[1] as $val){
if(strpos(str_replace(array('http://','https://'),'',$val),str_replace(array('http://','https://'),'',$domain))!==false){
$log_content = str_replace('src="'.$val.'"', 'src="'.str_replace(array('http:','https:'),'',$val).'"', $log_content);}}}
return $log_content;}?>


 方法二(直接跳转):也可以把外链本地化(ps:如果不懂,请直接使用上面的方法即可,本方法为本人自己使用) 链接: http://pan.baidu.com/s/1pLNTW4F(密码: g6ea),下载此文件放入程序根目录
将下面代码复制到module.php文件里去

<?php
function sl_wl($log_content,$domain){
preg_match_all('/hr ef="(.*?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val,$_SERVER['HTTP_HOST']) === false) {
$log_content = str_replace('hr ef="'.$val.'"', 'hr ef="'.BLOG_URL.'go/?url='.$val.'"', $log_content);
}else{return $log_content;}}}
preg_match_all('/src="(.*?)"/', $log_content, $matches);
if ($matches) {
foreach ($matches[1] as $val) {
if (strpos($val, $domain) === false) {
$log_content = str_replace('src="'.$val.'"', 'src="'.BLOG_URL.'go/?url='.$val.'"', $log_content);
}}}
return $log_content;
}?>


 温馨提示:把代码中的hr ef中的空格去掉,否则无效

调用方法:

在echo_log.php、page.php中用
<?php echo sl_wl($log_content,BLOG_URL);?>
替换
<?php echo $log_content; ?>
即可