<a href="/example.php?id=6&q=h">Link1</a>
<a href="/example.php?id=4&q=s">Link1</a>
<a href="/example.php?id=3&q=d">Link1</a>
<a href="/example.php?id=9&q=h">Link1</a>
<a href="/example.php?id=7&q=f">Link1</a>
<a href="/example.php?id=8&q=h">Link1</a>
所以,让我们假设这是一个包含100个链接的完整页面。我想只提取q = h的那些。我知道我可以遍历所有的标签然后使用if语句来查看q = h是否存在。但是,当有数百个链接时,这似乎不是很有效。感谢
答案 0 :(得分:0)
我个人会这样做
<?php
$htmlC = '<a href="/example.php?id=6&q=h">Link1</a>
<a href="/example.php?id=4&q=s">Link1</a>
<a href="/example.php?id=3&q=d">Link1</a>
<a href="/example.php?id=9&q=h">Link1</a>
<a href="/example.php?id=7&q=f">Link1</a>
<a href="/example.php?id=8&q=h">Link1</a>';
$dom = new DOMDocument();
@$dom->loadHTML($htmlC);
$xpath = new DOMXpath($dom);
$query = $xpath->query("//a[contains(@href, 'q=h')]");
foreach($query as $link){
//do what you need
}
?>