discuz X3.4帖子内站外链接加上 nofollow属性 理论支持 X2.5 X3 X3.1 X3.2 X3.3 X3.4版

打开目录source/function/function_discuzcode.php文件,查找parseurl函数,对照以下代码进行修改:

function parseurl($url, $text, $scheme) {
    global $_G;
    if(!$url && preg_match("/((https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|thunder|qqdl|synacast){1}:\/\/|www\.)[^\[\"']+/i", trim($text), $matches)) {
        $url = $matches[0];
        $length = 65;
        if(strlen($url) > $length) {
            $text = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));
        }
        $url = nofollow($url);
        return '<a href="'.(substr(strtolower($url), 0, 4) == 'www.' ? 'http://'.$url : $url).'" target="_blank">'.$text.'</a>';
    } else {
        $url = substr($url, 1);
        if(substr(strtolower($url), 0, 4) == 'www.') {
            $url = 'http://'.$url;
        }
        $url = !$scheme ? $_G['siteurl'].$url : $url;
        return '<a href="'.nofollow($url).'" target="_blank">'.$text.'</a>';
    }
}

在上面的代码下面 添加以下代码

function nofollow($url = '')
{
    $temp = array();

    if( ! empty($url))
    {
        $temp = parse_url($url);

        if(isset($temp['host']) && $temp['host'] != $_SERVER['HTTP_HOST'])
        {
            $url .= '" rel="nofollow"';
        }
    }

    unset($temp);
    return $url;
}
Last modification:June 27, 2019
如果觉得我的文章对你有用,请随意赞赏