使用PHP从DOM节点获取内容,但不从其子节点获取内容

Is it possible to use PHP's DOMXPath (or a similar alternative) to extract the value bar but not the value foo from the following structure?

<div>
    <p><span>foo</span>bar</p>
</div>

All of my attempts so far have returned content from the node's descendants as well as from the node itself, which is not what I want in this case.

The simple way is just to look for text nodes that are direct children of p:

$nodes = $xpath->query('//div/p/text()');

You may want a different selector in place of //div/, but the key bit is p/text(). / means "direct child nodes only" and text() means "text nodes only". So together they mean "direct children that are text nodes".

Credit should go to @lonesomeday since I updated my answer with his method:

$dom = new DOMDocument;
$dom->loadHTML('<div><p><span>foo</span>bar</p></div>');

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div/p/text()');

foreach($nodes as $node) {
    echo $node->nodeValue . '<br>';
}

Read this tutorial for more help with syntax.