【问题标题】:Extract div tags with specific class using PHP使用 PHP 提取具有特定类的 div 标签
【发布时间】:2012-10-20 17:07:22
【问题描述】:

有谁知道如何在 PHP 中只提取具有特定类的 div 标签?

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

我只需要 class="abc" 的 div。 我该如何实现?

【问题讨论】:

标签: php zend-framework dom


【解决方案1】:

你想要的是: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 查询。 试试看。

【讨论】:

  • 感谢您的回答。但它的给予并没有给我我需要的结果。
    DOMNodeList 对象 ( [length] => 0 )
  • 文件是否位于同一台服务器上?还是远程文件?
猜你喜欢
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 2021-07-18
相关资源
最近更新 更多