Extract div tags with specific class using PHP

2019-09-21 05:45发布

问题:

Does any one know how to extract only div tags with particular class,in PHP?

<div class="abc">
</div>
<div class="def">
</div>
<div class="xyz">
</div>

I need div only with class="abc". How can I implement that?

回答1:

What you want is: 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";
}
?>

[updated]

Added new xpath query. give that a try.