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);
}
}
?>