note 88795 modified in domnode.insertbefore by danbrown
| From: | danbrown@php.net | Date: | Sun, 08 Feb 2009 16:49:08 +0000 |
| Subject: | note 88795 modified in domnode.insertbefore by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150384@lists.php.net to get a copy of this message | ||
A very usual needed function is "appendSibling" to insert a node just before another one :
<?php
/**
* @param DOMNode $newnode Node to insert next to $ref
* @param DOMNode $ref Reference node
* @requires $ref has a parent node
* @return DOMNode the real node inserted
*/
function appendSibling(DOMNode $newnode, DOMNode $ref)
{
if ($ref->nextSibling) {
// $ref has an immediate brother : insert newnode before this one
return $ref->parentNode->insertBefore($newnode, $ref->nextSibling);
} else {
// $ref has no brother next to him : insert newnode as last child of his parent
return $ref->parentNode->appendChild($newnode);
}
}
?>
--was--
A very usual needed function is "appendSibling" to insert a node just before another one :
<?php
/**
* @param DOMNode $newnode Node to insert next to $ref
* @param DOMNode $ref Reference node
* @requires $ref has a parent node
* @return DOMNode the real node inserted
*/
function appendSibling(DOMNode $newnode, DOMNode $ref)
{
if ($ref->nextSibling) {
// $ref has an immediate brother : insert newnode before this one
return $ref->parentNode->insertBefore($newnode, $ref->nextSibling);
} else {
// $ref has no brother next to him : insert newnode as last child of his parent
return $ref->parentNode->appendChild($newnode);
}
}
http://php.net/manual/en/domnode.insertbefore.php