These two solutions should work:
$elements = $doc->getElementsByTagName('htmlText');
while ($elements->length > 0) {
$elements->item(0)->parentNode->removeChild($elements->item(0));
}
or loop backwards
$elements = $doc->getElementsByTagName('htmlText');
for ($i = $elements->length-1; $i >= 0; $i--) {
$elements->item($i)->parentNode->removeChild($elements->item($i));
}
Using foreach as suggested earlier, or looping from 0 up, won't work because the node list gets altered as you loop. You can test this with the following snippet:
$doc = new DOMDocument();
$doc->loadHTML('<p>first</p><p>second</p><p>third</p>');
foreach ($doc->getElementsByTagName('p') as $el) {
$el->parentNode->removeChild($el);
}
echo $doc->saveHTML();
Here the node list contains 3 elements: 0=>first, 1=>second, 2=>third. If you run it you'll see the second element is not removed because the first iteration removes the element at index 0 ('first'), leaving the node list with only 2 elements (0=>second, 1=>third). The next iteration removes the element at index 1 (third) and the loop ends. If you then save the document you'll find the second element remains untouched. Which is probably what you experienced when you said "it only deletes some of them" to the previous suggestion.