note 96167 added to domnode.removechild
| From: | drimsun at gmail dot com | Date: | Thu, 11 Feb 2010 23:37:18 +0000 |
| Subject: | note 96167 added to domnode.removechild | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165892@lists.php.net to get a copy of this message | ||
If you try to remove children of a nested node (e.g. a <ul> list that contains another
<ul>) and only remove the first level of child nodes, you'll get a messed-up node object.
Upon re-use of the messed-up node you'll get an error message that says: "Fatal error:
Call to a member function blahblah() on a non-object".
To remove ALL child nodes, use this recursive function instead:
<?php
/**
* Helper function that removes all child nodes of $node recursively
* (i.e. including child nodes of the child nodes and so forth).
*/
function remove_children(&$node) {
while ($node->firstChild) {
while ($node->firstChild->firstChild) {
remove_children($node->firstChild);
}
$node->removeChild($node->firstChild);
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 217.162.125.141
----
Manual Page -- http://www.php.net/manual/en/domnode.removechild.php
Edit -- https://master.php.net/note/edit/96167
Del: integrated -- https://master.php.net/note/delete/96167/integrated
Del: useless -- https://master.php.net/note/delete/96167/useless
Del: bad code -- https://master.php.net/note/delete/96167/bad+code
Del: spam -- https://master.php.net/note/delete/96167/spam
Del: non-english -- https://master.php.net/note/delete/96167/non-english
Del: in docs -- https://master.php.net/note/delete/96167/in+docs
Del: other reasons-- https://master.php.net/note/delete/96167
Reject -- https://master.php.net/note/reject/96167
Search -- https://master.php.net/manage/user-notes.php