Extract div tags with specific class using PHP

2019-09-21 05:46发布

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条回答
女痞
2楼-- · 2019-09-21 06:28

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.

查看更多
登录 后发表回答