如何添加rel ="nofollow"?链接到preg_replace() [英] How to add rel="nofollow" to links with preg_replace()

查看:84
本文介绍了如何添加rel ="nofollow"?链接到preg_replace()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数旨在将rel="nofollow"属性应用于所有外部链接,而没有内部链接,除非该路径与以下定义为$my_folder的预定义根URL匹配.

The function below is designed to apply rel="nofollow" attributes to all external links and no internal links unless the path matches a predefined root URL defined as $my_folder below.

所以给定变量...

$my_folder = 'http://localhost/mytest/go/';
$blog_url = 'http://localhost/mytest';

还有内容...

<a href="http://localhost/mytest/">internal</a>

<a href="http://localhost/mytest/go/hostgator">internal cloaked link</a>

<a href="http://cnn.com">external</a>

更换后的最终结果应该是...

The end result, after replacement should be...

<a href="http://localhost/mytest/">internal</a>

<a href="http://localhost/mytest/go/hostgator" rel="nofollow">internal cloaked link</a>

<a href="http://cnn.com" rel="nofollow">external</a>

请注意,第一个链接没有更改,因为它是内部链接.

Notice that the first link is not altered, since its an internal link.

第二行上的链接也是一个内部链接,但是由于它与我们的$my_folder字符串匹配,所以它也获得了nofollow.

The link on the second line is also an internal link, but since it matches our $my_folder string, it gets the nofollow too.

第三个链接是最简单的,因为它与blog_url不匹配,它显然是一个外部链接.

The third link is the easiest, since it does not match the blog_url, its obviously an external link.

但是,在下面的脚本中,我所有的链接都变为nofollow.如何修复脚本以执行我想要的操作?

However, in the script below, ALL of my links are getting nofollow. How can I fix the script to do what I want?

function save_rseo_nofollow($content) {
$my_folder =  $rseo['nofollow_folder'];
$blog_url = get_bloginfo('url');
    preg_match_all('~<a.*>~isU',$content["post_content"],$matches);
    for ( $i = 0; $i <= sizeof($matches[0]); $i++){
        if ( !preg_match( '~nofollow~is',$matches[0][$i])
            && (preg_match('~' . $my_folder . '~', $matches[0][$i]) 
               || !preg_match( '~'.$blog_url.'~',$matches[0][$i]))){
            $result = trim($matches[0][$i],">");
            $result .= ' rel="nofollow">';
            $content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
        }
    }
    return $content;
}

推荐答案

尝试首先使其更具可读性,然后再使您的if规则变得更加复杂:

Try to make it more readable first, and only afterwards make your if rules more complex:

function save_rseo_nofollow($content) {
    $content["post_content"] =
    preg_replace_callback('~<(a\s[^>]+)>~isU', "cb2", $content["post_content"]);
    return $content;
}

function cb2($match) { 
    list($original, $tag) = $match;   // regex match groups

    $my_folder =  "/hostgator";       // re-add quirky config here
    $blog_url = "http://localhost/";

    if (strpos($tag, "nofollow")) {
        return $original;
    }
    elseif (strpos($tag, $blog_url) && (!$my_folder || !strpos($tag, $my_folder))) {
        return $original;
    }
    else {
        return "<$tag rel='nofollow'>";
    }
}

给出以下输出:

[post_content] =>
  <a href="http://localhost/mytest/">internal</a>
  <a href="http://localhost/mytest/go/hostgator" rel=nofollow>internal cloaked link</a>    
  <a href="http://cnn.com" rel=nofollow>external</a>

您原始代码中的问题可能是$ rseo,未在任何地方声明.

The problem in your original code might have been $rseo which wasn't declared anywhere.

这篇关于如何添加rel ="nofollow"?链接到preg_replace()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆