在“href”标记中解析包含特定单词的所有链接

时间:2011-11-21 06:51:27

标签: php parsing

  

可能重复:
  Grabbing the href attribute of an A element

我需要解析包含某些单词的HTML文档的所有链接(它总是不同的)。

示例:

<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>

我只需要带有“href = / link:....”的链接,最好的方法是什么?

$html = "SOME HTLM ";
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
foreach ($urls as $url)
{
    echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
    echo "<hr><br>";
}

在此示例中显示了所有链接,我需要特定的链接。

4 个答案:

答案 0 :(得分:5)

使用条件。

<?php 
$lookfor='/link:';

foreach ($urls as $url){
    if(substr($url->getAttribute('href'),0,strlen($lookfor))==$lookfor){
        echo "<br> ".$url->getAttribute('href')." , ".$url->getAttribute('title');
        echo "<hr><br>";
    }
}
?>

答案 1 :(得分:3)

不是首先获取所有元素然后过滤掉您需要的元素,而是可以使用XPath直接在文档中查找这些节点:

//a[contains(@href, "link:")]

此查询将在文档中找到所有元素,其中包含 href属性 href属性 >。

要检查href属性是否以链接开头:您可以

//a[starts-with(@href, "link:")]

完整示例(demo):

$dom = new DomDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//a[contains(@href, "link:")]') as $a) {
    echo $a->getAttribute('href'), PHP_EOL;
}

请同时参阅

相关问题。

注意:由于许多相关问题而标记此CW

答案 2 :(得分:0)

使用正则表达式。

foreach ($urls as $url)
{
    $href = $url->getAttribute('href');
    if (preg_match("/^\/link:/",$href){
        $links[$url->getAttribute('title')] = $href;
    }
}

$ links数组包含所有匹配的标题和href。

答案 3 :(得分:0)

由于getAttribute只返回一个字符串,你只需要用strpos()检查它的开头。

$href = $url -> getAttrubute ('href');
if (strpos ($href, '/link:') === 0)
{
    // Do your processing here
}