开发者

Can I get siblings with PHP's DOMDocument?

开发者 https://www.devze.com 2023-01-30 09:30 出处:网络
I have gotten some h3 references in PHP\'s DOMDocument. $dom = new DOMDocument(); $dom->loadHtml($content);

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.


$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

0

精彩评论

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

关注公众号