It should just be $html->find('div.right > text') , but that won't work, because plain HTML DOM Parser doesn't seem to support direct child queries.
So, first you need to find all the <div> elements and find the child nodes for the text node. Unfortunately, the ->childNodes() method maps to ->children() and therefore only returns items.
The working solution is to call ->find('text') for each <div> element, after which you filter the results based on the parent node.
foreach ($doc->find('div.right') as $parent) { foreach ($parent->find('text') as $node) { if ($node->parent() === $parent && strlen($t = trim($node->plaintext))) { echo $t, PHP_EOL; } } }
Using a DOMDocument , this XPath expression will do the same job without pain:
$doc = new DOMDocument; $doc->loadHTML($content); $xp = new DOMXPath($doc); foreach ($xp->query('//div/text()') as $node) { if (strlen($t = trim($node->textContent))) { echo $t, PHP_EOL; } }
source share