note 56556 added to function.dom-domnode-removechild

From: Date: Wed, 07 Sep 2005 09:20:23 +0000
Subject: note 56556 added to function.dom-domnode-removechild
Groups: php.notes 
Request: Send a blank email to php-notes+get-94850@lists.php.net to get a copy of this message
Back again on removing childs and iterators robustness. Things get a bit more complicated when you only want to remove 'some' nodes according to a certain condition. Then you can't just remove the first one repeatedly. The trick is to copy the content of the node list into a more robust collection than DOMNodeList, I name array! The following piece of code will, for instance, remove all empty child nodes: <?php // Copy childNodes array $childNodes = array(); foreach($node->childNodes as $childNode) { $childNodes[] = $childNode; } // Browse with the copy foreach ($childNodes as $childNode) { if (!$childNode->hasChildNodes()); { $childNode->parentNode->removeChild($childNode); } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.dom-domnode-removechild.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56556 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56556&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56556&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94850) next »