Well, the problem is that you are updating the iterator $bodyChildren(this is not an array, this is an object DomNodeList) when you iterate over it. Instead, try doing this:
while ($bodyChildren->length > 0) {
$body->removeChild($bodyChildren->item(0));
}
It seems a little back, but it should work for your needs ...
source
share