您的位置:首页 > 其它

给Discuz X论坛帖子的外部链接增加nofollow属性

2014-01-06 15:57 417 查看
给Discuz X论坛的外部链接增加nofollow方法,测试版本Discuz! X3.1,其它版本未经测试

打开目录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>';
}
}

在parseurl函数后面新增nofollow函数,代码如下:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: