Can I get siblings using PHP DOMDocument?

I got some h3 links in PHP DOMDocument.

 $dom = new DOMDocument(); $dom->loadHtml($content); $h3s = $dom->getElementsByTagName('h3'); foreach($h3s as $h3) { var_dump($h3->nodeValue); } 

I need to get the following items after h3 s. In this case, all elements will correspond to the next h3 or the end of the document.

It worked easily with regex, but I don't want to use it here to parse HTML.

For reference, this is a regular expression ...

 preg_match_all('/<h3>([^<]+)<\/h3>(.*?)(<h3|$)/', $content, $matches); 

(which is fragile, therefore, a desire for proper parsing).

So, how can I use the DOMDOcument data that I expect in $matches from the regular expression above?

I checked the documentation but could not find the equivalent of the nextSibling JavaScript nextSibling .

+7
source share
1 answer

$h3->nextSibling and $h3->previousSibling are what you are looking for.

getElementsByTagName returns a DOMNodeList containing DOMNode elements when you DOMNode over it.

http://www.php.net/manual/en/class.domnode.php

+14
source

All Articles