Search code examples
phpdomdomdocumentdomxpath

DOMDocument get nodeValue of each matching element


I've been hacking at this for a while and just cant seem to get it right.

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:

<div></div>
<iframe><iframe>
<script>xxxx</script>
<script>xxxx</script>
<script>xxxx</script>

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 DOMXPath documentation as how to do it. This is what I have so far:

$dom = new DOMDocument();
$dom->preserveWhiteSpace = true;
@$dom->loadHtml($form_content);
$xpath = new DOMXPath($dom);
$items = $xpath->query('//script');
foreach ($items as $item) {
     $scriptContents = $item->previousSibling->previousSibling->nodeValue . "\r\    n\r\n";
     $scriptContents .= $item->previousSibling->nodeValue . "\r\n\r\n";
     $scriptContents .= $item->nodeValue . "\r\n\r\n";
}
echo $scriptContents;

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


Solution

  • It appears that you are overwriting $scriptContents with each iteration, which is probably not what you are intending. The way the script currently is operating, your output would be limited to the two previous siblings of the last script tag (whether or not they are actually script tags themselves) along with the last script tag.

    If you are strictly trying to output the script tags you can do this:

    $xpath = new DOMXPath($dom);
    $items = $xpath->query('//script');
    foreach ($items as $item) {
         echo $item->nodeValue . "\r\n\r\n";
    }