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

Ron*_*Ron 2 php parsing

可能重复:
抓取A元素的href属性

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

例:

<a href="/bla:bla">BLA</a>
<a href="/link:link">BLA</a>
<a href="/link:bla">BLA</a>
Run Code Online (Sandbox Code Playgroud)

我只需要带有"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>";
}
Run Code Online (Sandbox Code Playgroud)

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

Law*_*one 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>";
    }
}
?>
Run Code Online (Sandbox Code Playgroud)