简体   繁体   English

DOM文档-如何在不带内部标签的标签内获取文本

[英]DOM Document - How to get the text inside a tag without the inner tags

Assume I have a dom_document containing the following html and it is put in a variable called $dom_document 假设我有一个包含以下html的dom_document,并将其放在名为$dom_document的变量中

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

What i would like is retrieve the text that is inside the div tag ('I want this'), but not the a tag. 我想要的是检索div标签(“我想要这个”)中的文本,而不是a标签。 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

You can use XPath for it: 您可以使用XPath:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM