title: DOMDocument get nodeValue of each matching element
body: <p>I've been hacking at this for a while and just cant seem to get it right.</p>
  
  <p>How can you get get the contents of all script elements, when the number of script elements is variable. My example markup looks like this:</p>
  
  <pre><code>&lt;div&gt;&lt;/div&gt;
  &lt;iframe&gt;&lt;iframe&gt;
  &lt;script&gt;xxxx&lt;/script&gt;
  &lt;script&gt;xxxx&lt;/script&gt;
  &lt;script&gt;xxxx&lt;/script&gt;
  </code></pre>
  
  <p>What I have so far works only if I keep the number of scripts static so clearly Im not iterating over the array correctly, but Im totally thrown by the <a href="http://php.net/manual/en/class.domxpath.php" rel="nofollow">DOMXPath documentation</a> as how to do it. This is what I have so far:</p>
  
  <pre><code>$dom = new DOMDocument();
  $dom-&gt;preserveWhiteSpace = true;
  @$dom-&gt;loadHtml($form_content);
  $xpath = new DOMXPath($dom);
  $items = $xpath-&gt;query('//script');
  foreach ($items as $item) {
       $scriptContents = $item-&gt;previousSibling-&gt;previousSibling-&gt;nodeValue . "\r\    n\r\n";
       $scriptContents .= $item-&gt;previousSibling-&gt;nodeValue . "\r\n\r\n";
       $scriptContents .= $item-&gt;nodeValue . "\r\n\r\n";
  }
  echo $scriptContents;
  </code></pre>
  
  <p>How should I go about this? I've been search SO for a while now, but can seem to apply a solution that works. Thanks in advance - b </p>