(PHP 8 >= 8.4.0)
DOMNode::compareDocumentPosition — 比较两个节点的位置
other
要比较其位置的节点,相对于此节点。
DOMNode::DOCUMENT_POSITION_*
常量的位掩码。
示例 #1 DOMNode::compareDocumentPosition() 示例
<?php
$xml = <<<XML
<root>
<child1/>
<child2/>
</root>
XML;
$dom = new DOMDocument();
$dom->loadXML($xml);
$root = $dom->documentElement;
$child1 = $root->firstElementChild;
$child2 = $child1->nextElementSibling;
var_dump($root->compareDocumentPosition($child1));
var_dump($child2->compareDocumentPosition($child1));
?>
以上示例将输出
int(20) // This is DOMNode::DOCUMENT_POSITION_CONTAINED_BY | DOMNode::DOCUMENT_POSITION_FOLLOWING int(2) // This is DOMNode::DOCUMENT_POSITION_PRECEDING