DOM文档-如何在不带内部标签的标签内获取文本 [英] DOM Document - How to get the text inside a tag without the inner tags

查看:51
本文介绍了DOM文档-如何在不带内部标签的标签内获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含以下html的dom_document,并将其放在名为 $ dom_document

Assume I have a dom_document containing the following html and it is put in a variable called $dom_document

<div>
  <a href='something'>some text here</a>
  I want this
</div>

我想要的是检索div标记内的文本(我想要这个"),而不是a标记.我的工作如下:

What i would like is retrieve the text that is inside the div tag ('I want this'), but not the a tag. What i do is the following:

$dom_document->nodeValue;

不幸的是,在此声明中,我带有一个标记.希望有人能帮忙.先感谢您.干杯.马克

Unfortunately with this statement I have the a tag in with it. Hope someone can help. Thank you in advance. Cheers. Marc

推荐答案

您可以为其使用XPath:

You can use XPath for it:

$xpath = new DOMXpath($dom_document);
$textNodes = $xpath->query('//div/text()');

foreach ($textNodes as $txt) {
    echo $txt->nodeValue;
}

这篇关于DOM文档-如何在不带内部标签的标签内获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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