Loop over DOMDocument
- by Zoredache
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