DOMNode::compareDocumentPosition

Compares the position of two nodes

説明

public int DOMNode::compareDocumentPosition(DOMNode $other)

Compares the position of the other node relative to this node.

パラメータ

other

The node for which the position should be compared for, relative to this node.

戻り値

A bitmask of the DOMNode::DOCUMENT_POSITION_* constants.

例1 DOMNode::compareDocumentPosition example

<?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