使用PHP进行DOM操作 [英] DOM Manipulation with PHP

查看:178
本文介绍了使用PHP进行DOM操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP对DOM Elements进行简单但非琐碎的操作,但我迷失了.

I would like to make a simple but non trivial manipulation of DOM Elements with PHP but I am lost.

假设有一个类似Wikipedia的页面,其中有段落和标题(<p><h2>).他们是兄弟姐妹.我想按顺序考虑这两个要素.

Assume a page like Wikipedia where you have paragraphs and titles (<p>, <h2>). They are siblings. I would like to take both elements, in sequential order.

我已经尝试过GetElementbyName,但是那样您就不可能组织信息了. 我尝试过DOMXPath->query(),但发现它确实令人困惑.

I have tried GetElementbyName but then you have no possibility to organize information. I have tried DOMXPath->query() but I found it really confusing.

只需解析如下内容:

<html>
  <head></head>
  <body>
    <h2>Title1</h2>
    <p>Paragraph1</p>
    <p>Paragraph2</p>
    <h2>Title2</h2>
    <p>Paragraph3</p>
  </body>
</html>

进入:


Title1
Paragraph1
Paragraph2
Title2
Paragraph3

使用一点点HTML代码,我不需要所有这些代码.

With a few bits of HTML code I do not need between all.

谢谢.我希望问题看起来不像是家​​庭作业.

Thank you. I hope question does not look like homework.

推荐答案

我认为DOMXPath->query()是正确的方法.该XPath表达式将返回同一级别上的所有<h2><p>节点(因为您说过它们是同级的).

I think DOMXPath->query() is the right approach. This XPath expression will return all nodes that are either a <h2> or a <p> on the same level (since you said they were siblings).

/html/body/*[name() = 'p' or name() = 'h2']

节点将作为节点列表返回.正确的顺序(文档顺序).然后,您可以在结果上构造一个foreach循环.

The nodes will be returned as a node list in the right order (document order). You can then construct a foreach loop over the result.

这篇关于使用PHP进行DOM操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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