ale*_*lex 8 html php domdocument
我h3在PHP的DOMDocument中得到了一些引用.
$dom = new DOMDocument();
$dom->loadHtml($content);
$h3s = $dom->getElementsByTagName('h3');
foreach($h3s as $h3) {
var_dump($h3->nodeValue);
}
Run Code Online (Sandbox Code Playgroud)
我需要在h3s 之后获得下一个元素.在这种情况下,它将是h3文档的下一个或结尾的所有元素.
它使用正则表达式很容易,但我不想在这里使用它来解析HTML.
作为参考,那个正则表达式是......
preg_match_all('/<h3>([^<]+)<\/h3>(.*?)(<h3|$)/', $content, $matches);
Run Code Online (Sandbox Code Playgroud)
(这是脆弱的,因此需要正确的解析).
那么我如何$matches从上面的正则表达式中使用DOMDOcument我期望的数据呢?
我检查了文档,但找不到相同的JavaScript nextSibling属性.
jim*_*myi 15
$h3->nextSibling而且$h3->previousSibling正是你要找的.
getElementsByTagName返回一个DOMNodeList,DOMNode当你迭代它时,它包含元素.
http://www.php.net/manual/en/class.domnode.php