If you want to save without the XML declaration, and LIBXML_NOXMLDECL doesn't work for you, you can just do this:
<?php
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->loadXML($xml, LIBXML_*);
echo $doc->saveXML($doc->firstElementChild);
?>
This will output the XML without the XML declaration and without using the flag.
You also don't need to do fiddly replacements and pray that it works.