我想知道如何通过PHP REGEX识别URL中的Nofollow关系。
<a href="abc.html" rel="NOFOLLOW">How to check NOFOLLOW<a>请给我解决这个问题的办法
发布于 2011-11-24 14:14:55
你可以尝试像这样的东西。
preg_match('/<a.+?rel="nofollow".*?>[\s\S]*?<\/a>/i', $html);CodePad。
但是,你最好使用一个超文本标记语言解析器,它可以处理正则表达式不能处理的事情。
$dom = new DOMDocument;
$dom->loadHTML($html);
$anchors = $dom->getElementsByTagName('a');
foreach($anchors as $anchor) {
if ($anchor->hasAttribute('rel')) {
$rel = preg_split('/\s+/', strtolower($anchor->getAttribute('rel')));
if (in_array('nofollow', $rel)) {
echo 'This anchor is "nofollow"\'d.';
}
}
}CodePad。
https://stackoverflow.com/questions/8252874
复制相似问题