Loop over DOMDocument

Posted by Zoredache on Stack Overflow See other posts from Stack Overflow or by Zoredache
Published on 2010-05-26T02:51:54Z Indexed on 2010/05/26 3:01 UTC
Read the original article Hit count: 406

Filed under:
|
|

I am following the suggestion from this question Robust, Mature HTML Parser for PHP, about parsing html that may be malformed with DOMDocument.

Is there any easy way to loop over the parsed document? So I would like to loop over html like this.

$html='<ul>
         <li>value1</li>
         <li>value1</li>
         <li>value3</li>
        </ul>
        <p>hello world</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);
???
foreach (??? as $node)
{
  print $node->nodeName.':'.$node->nodeValue;
}

And get results somewhat like this.

 ul:
 li:value1
 li:value2
 li:value3
 p:hello world

© Stack Overflow or respective owner

Related posts about php

Related posts about loop