Question

I have a block of html in a string that is basically a list of divs... Each div has html inside that I want to parse seperately.

I am having trouble figuring out exactly how to loop over the initial divs.

Can anyone help?

An example of the html:

<div><!-- stuff in here --></div>
<div><!-- stuff in here --></div>
<div><!-- stuff in here --></div>
<div><!-- stuff in here --></div>

In this example I would expect the final code to loop round 4 times and provide me with the contents of each div

Was it helpful?

Solution

This should work (if the HTML is in an external file):

$doc = new DOMDocument();
$doc->loadHTMLFile('test.html');
$divs = $doc->getElementsByTagName('div');
foreach($divs as $n) {
    echo $n->nodeValue;
}

And in case of a string containing the HTML, you could do:

$doc = new DOMDocument();
$doc->loadHTML('<html><body><div>A</div><div>B</div><div>C</div><div>D</div></body></html>');
$divs = $doc->getElementsByTagName('div');
foreach($divs as $n) {
  echo $n->nodeValue . "\n";
}

which would produce:

A
B
C
D

OTHER TIPS

If it's XHTML, you can use SimpleXML:

$xml = simplexml_load_string($xhtmlstring);
foreach ($xml->div as $d) {
   {
   //parsing
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top