views:

22

answers:

2

Hello,

In CodeIgniter, I'm writing an XML file using write_file function.

write_file('data.xml', $str_xml);

But the encoding of the file is always ANSI. How can I have this file be encoded to UTF-8 automatically?

+1  A: 

I don't know the function in question, but I assume it will write out the bytes it receives. Except for the BOM, there is no defined indicator how a file is encoded: It will depend on what you open the file as.

UTF-8 encoding will look like Ansi for the first 127 characters of the ASCII table, and switch to multi-byte characters when encountering characters outside that. It is therefore perfectly possible that a UTF-8 encoded file looks totally identical to an ANSI one if it doesn't contain any special characters.

I can be wrong, but my suspicion is the file is UTF-8 but whatever you're using to test the encoding with is giving you the wrong result. Could that be?

Pekka
A: 

OK, thanks for any suggestion. I've just figured it out myself. What I need is the PHP function called "utf8_encode()". Like this...

write_file('data.xml', utf8_encode($str_xml));

Now the file is saved to UTF-8 automatically.

Jack101