开发者

Can Codeigniter write_file write a UTF-8 file?

开发者 https://www.devze.com 2023-01-05 15:14 出处:网络
In CodeIgniter, I\'m writing an XML file using write_file function. write_file(\'data.xml\', $str_xml);

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 开发者_Python百科automatically?


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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消