PHP DOMDocument UTF-8 data into ISO-8859-1 document snippets

Encoding is hard in many cases. Today I ran into one, where we had to save UTF-8 data (from a Drupal website) into an XML document which requires ISO-8859-1 encoding.

We're using https://www.php.net/manual/de/class.domdocument.php for that, which seems to have some problems / flaws with such cases, as the following issues show:

After trying conversion of the relevant data using utf8_decode and other ways like iconv and mb_convert_encoding I'm now finally using this (ugly) workaround which seems to give us the expected results.
Perhaps it also helps in your case, if it's similar:


  1. $domtree = new DOMDocument('1.0', 'ISO-8859-1');
  2. $domtree->createElement("example123");
  3. // We do NOT use the DOMDocument definition here, instead build our own:
  4. $xmlString = '<?xml version="1.0" encoding="ISO-8859-1"
' . PHP_EOL . $domtree->saveXML($domtree->documentElement);
?>

All other possible solutions resulted in wrong encoding for me. I guess the reason is, that DOMDocument uses internal conversion magic...