Question

I'm trying to output XML file using PHP, and everything is right except that the file that is created isn't UTF-8 encoded, it's ANSI. (I see that when I open the file an do the Save as...). I was using

$dom = new DOMDocument('1.0', 'UTF-8');

but I figured out that non-english characters don't appear on the output. I was searching for solution and I tryed first adding

header("Content-Type: application/xml; charset=utf-8");

at the beginning of the php script but it say's: Extra content at the end of the document Below is a rendering of the page up to the first error.

I've tryed some other suggestions like not to include 'UTF-8' when creating the document but to write it separately: $doc->encoding = 'UTF-8'; , but the result was the same.

I used

$doc->save("filename.xml"); 

to save the file, and I've tryed to change it to

$doc->saveXML();

but the non-english characters didn't appear. Any ideas?

Was it helpful?

Solution

ANSI is not a real encoding. It's a word that basically means "whatever encoding my Windows computer is configured to use". Getting ANSI is a clear sign of relying on default encoding somewhere.

In order to generate valid UTF-8 output, you have to feed all XML functions with proper UTF-8 input. The most straightforward way to do it is to save your PHP source code as UTF-8 and then just type some non-English letters. If you are reading data from external sources (such as a database) you need to ensure that the complete toolchain makes proper use of encodings.

Whatever, using "Save as" in an undisclosed piece of software is not a reliable way to determine the file encoding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top