Loop through
elements using PHP

2020-07-24 08:11发布

问题:

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

回答1:

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


回答2:

If it's XHTML, you can use SimpleXML:

$xml = simplexml_load_string($xhtmlstring);
foreach ($xml->div as $d) {
   {
   //parsing
   }
}