I have the following (PHP) code that traverses the entire DOM document to get all the text nodes. This is a bit ugly decision, and I'm sure there must be a better way ... so, is there?
$skip = false; $node = $document; $nodes = array(); while ($node) { if ($node->nodeType == 3) { $nodes[] = $node; } if (!$skip && $node->firstChild) { $node = $node->firstChild; } elseif ($node->nextSibling) { $node = $node->nextSibling; $skip = false; } else { $node = $node->parentNode; $skip = true; } }
Thanks.
source share