protected function removeComments(DOMDocument $document) { $xpath = new DOMXpath($document); $nodes = $xpath->query('//comment()'); for ($i = 0; $i < $nodes->length; ++$i) { $nodes->item($i)->parentNode->removeChild($nodes->item($i)); } }