是否有人知道如何只提取具有特定類的div標記,在PHP中?使用PHP提取具有特定類的div標記
<div class="abc">
</div>
<div class="def">
</div>
<div class="xyz">
</div>
我只需要div class =「abc」。 我該如何實現?
是否有人知道如何只提取具有特定類的div標記,在PHP中?使用PHP提取具有特定類的div標記
<div class="abc">
</div>
<div class="def">
</div>
<div class="xyz">
</div>
我只需要div class =「abc」。 我該如何實現?
你想要的是:XPath
<?php
$html = new DOMDocument();
@$html->loadHtmlFile('thepage.html');
$xpath = new DOMXPath($html);
$nodelist = $xpath->query("//*[contains(@class, 'abc')]");
foreach ($nodelist as $n){
echo $n->nodeValue."\n";
}
?>
[更新]
增加了新的XPath查詢。 試試看。
可能要求[在PHP中解析HTML的最佳方法](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php),但目前NARQ。 – mario
嘗試使用DOM,http://php.net/manual/en/book.dom.php –