自定义php函数,给文章添加 nofollow 标签

198次阅读
没有评论

/**
 * 给文章添加 nofollow 标签
 * @param $content 文章内容
 * @return string  返回替换后的内容
 */
function cn_nf_url_parse($content)
{$regexp = '<a[^>]*href=(" ??)([^" >]*?)\1[^>]*>';
    if (preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {if (!empty($matches)) {$srcUrl = config('app.url');
            for ($i = 0; $i < count($matches); $i++) {$tag = $matches[$i][0];
                $tag2 = $matches[$i][0];
                $url = $matches[$i][0];
                $noFollow = '';
                $pattern = '/targets*=s*"s*_blanks*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if (count($match) < 1)
                    $noFollow .= ' target="_blank" ';
                $pattern = '/rels*=s*"s*[n|d]ofollows*"/';
                preg_match($pattern, $tag2, $match, PREG_OFFSET_CAPTURE);
                if (count($match) < 1)
                    $noFollow .= ' rel="nofollow" ';

                $pos = strpos($url, $srcUrl);
                if ($pos === false) {$tag = rtrim($tag, '>');
                    $tag .= $noFollow . '>';
                    $content = str_replace($tag2, $tag, $content);
                }
            }
        }
    }
    $content = str_replace(']]>', ']]>', $content);
    return $content;
}
正文完
有偿技术支持加微信
post-qrcode
 
评论(没有评论)
验证码