不要用这代码,没写好,会重复添加target="_blank",懒得修复了,如果需要,修改成正则替换整个a标签就行。

下方代码放进e/class/userfun.php文件里:

// 外链nofollow和target="_blank"
function cn_nf_url_parse($content){
    $content = stripSlashes($content);
    preg_match_all('/href="(http.*?)"/',$content,$matches);
    if($matches){
        foreach($matches[1] as $val){
            if( strpos($val,$_SERVER['HTTP_HOST'])===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val))
            $content=str_replace("href=\"$val\"", "rel=\"external nofollow\" target=\"_blank\" href=\"$val\"",$content);
        }
    }
    return $content;
}

内容模版里[!--newstext--]换成下方代码:

<?=cn_nf_url_parse($navinfor['newstext'])?>

进阶:支持白名单

// 外链nofollow和target="_blank",支持白名单
function cn_nf_url_parse($content){
    $content = stripSlashes($content);
    $array = array("baidu.com","so.com");// 白名单
    preg_match_all('/href="(http.*?)"/',$content,$matches);
    if($matches){
        foreach($matches[1] as $val){
            if( strpos($val,$_SERVER['HTTP_HOST'])===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
                $mode = false;
                foreach($array as $Whitelist){
                    if(strpos($val,$Whitelist)){
                        $mode = true;
                        break;     
                    }
                }
                if( $mode===false){
                    $content=str_replace("href=\"$val\"", "rel=\"external nofollow\" target=\"_blank\" href=\"$val\" ",$content);
                }
            }
        }
     }
    return $content;
}