使用PHP使用特定类提取div标签 [英] Extract div tags with specific class using PHP

查看:828
本文介绍了使用PHP使用特定类提取div标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何在PHP中提取特定类的div标签?

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>

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

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

推荐答案

你想要的是: 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查询。
给了一个尝试。

Added new xpath query. give that a try.

这篇关于使用PHP使用特定类提取div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆