$html为文章内容,处理了网站内部的a链接不添加nofollow,外部的a链接添加nofollow
function webOutUrlDispose($html)
{
$host = [$_SERVER['HTTP_HOST']];//站点host
preg_match_all('/href="(.*?)"/', $html, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
//如果url里面没有://跳出本次循环
if (!strstr($matches[1][$i], '://')) {
continue;
}
$array = parse_url($matches[1][$i]);
//如果是内链的地址,跳出本次循环
if (in_array($array['host'], $host)) {
continue;
}
//将外链的href替换为 rel="nofollow" href
if (!strstr($matches[0][$i], 'rel=')) {
$yuan = $matches[0][$i];
$matches[0][$i] = str_replace('href', ' rel="nofollow" href', $matches[0][$i]);
$html = str_replace($yuan, $matches[0][$i], $html);
}
}
return $html;
}
给文章里面的外链a标签添加nofollow
最新推荐文章于 2021-09-10 10:29:54 发布