如何通过 DOMDocument 获取元素的正文

How to get the main text of an element by DOMDocument

我想获取不带 DOMDocument 子元素的元素的主文本。例如,

<span>
This is the main
    <i>more</i>
    <b>extra</b>
<span>

我通过

获取文本
$text=$g->query('//span')[0]->nodeValue;

但是我怎样才能只得到span下面的文本值呢?在这里,只有 This is the main 文本忽略任何子元素。

使用text() node test:

echo $g->query('//span/text()')[0]->nodeValue;

输出为:

This is the main

这只会找到 <span>.

的直接子节点的文本节点