title: DOM: fetch all text nodes in the document (PHP)
body: <p>I've have the following (PHP) code that traverses an entire DOM document to get all of the text nodes. It's a bit of a ugly solution, and I'm sure there must be a better way... so, is there?</p>
  
  <pre><code>$skip = false;
  $node = $document;
  $nodes = array();
  while ($node) {
      if ($node-&gt;nodeType == 3) {
          $nodes[] = $node;
      }
      if (!$skip &amp;&amp; $node-&gt;firstChild) {
          $node = $node-&gt;firstChild;
      } elseif ($node-&gt;nextSibling) {
          $node = $node-&gt;nextSibling;
          $skip = false;
      } else {
          $node = $node-&gt;parentNode;
          $skip = true;
      }
  }
  </code></pre>
  
  <p>Thanks.</p>