Extract div tags with specific class using PHP

别来无恙 提交于 2019-12-26 21:41:43

问题


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.



来源:https://stackoverflow.com/questions/13154595/extract-div-tags-with-specific-class-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!