note 49945 added to function.dom-domnode-removechild

From: Date: Sun, 13 Feb 2005 11:35:11 +0000
Subject: note 49945 added to function.dom-domnode-removechild
Groups: php.notes 
Request: Send a blank email to php-notes+get-84916@lists.php.net to get a copy of this message
For those who don’t understand >sbeam at onsetcorps dot net on 02-Feb-2005 12:07< “hack”, here’s a little discussion: First but *wrong* try would be: <?php foreach ($parent->childNodes as $child) { $parent->removeChild($child); ?> This doesn’t work because DOM tree id modified on-the-fly and this confuses foreach loop. The idea behind sbeam’s trick is that after removing the first item in the first iteration, the second item in childNodes nodelist immediately becomes the first item. That’s why we must _always_ remove the first child. Here’s another implementation: <?php $count = $parent->childNodes->length; for ($i = 0; $i < $count; $i++) { $oldNode = $parent->removeChild($parent->childNodes->item(0)); // !!! not item($i) !!! } ?> ---- Manual Page -- http://www.php.net/manual/en/function.dom-domnode-removechild.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+49945 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49945&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49945&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49945&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49945&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49945&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+49945&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#84916) next »