Can I get siblings with PHP's DOMDocument?

alex picture alex · Dec 15, 2010 · Viewed 8.2k times · Source

I have gotten some h3 references in PHP's DOMDocument.

$dom = new DOMDocument();

$dom->loadHtml($content);

$h3s = $dom->getElementsByTagName('h3');

foreach($h3s as $h3) {

    var_dump($h3->nodeValue);

}

I need to get the next elements after the h3s. In this case, it will be all elements up to the next h3 or end of document.

It worked easily with a regular expression, but I don't want to use one here to parse HTML.

For reference, that regular expression is...

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

(Which is fragile, hence the desire for proper parsing).

So how would I get using DOMDOcument the data I expect in $matches from the regular expression above?

I checked the documentation, but couldn't find a equivalent of JavaScript's nextSibling property.

Answer

jimyi picture jimyi · Dec 15, 2010

$h3->nextSibling and $h3->previousSibling is what you're looking for.

getElementsByTagName returns a DOMNodeList, which contains DOMNode elements when you iterate over it.

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