Search code examples
phpdomxpathdomxpath

Using PHP to fetch content from a DOM node but not from its children


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.


Solution

  • 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".