开发者

Loop through <div> elements using PHP

开发者 https://www.devze.com 2022-12-16 15:47 出处:网络
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 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


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


If it's XHTML, you can use SimpleXML:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号