Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get siblings with PHP's DOMDocument?

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.

like image 882
alex Avatar asked Dec 15 '10 00:12

alex


1 Answers

$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

like image 105
jimyi Avatar answered Nov 15 '22 14:11

jimyi