how to convert from Unicode to UTF-8 in PHP?

2019-08-30 18:27发布

I saved file named "File1.txt" with the content (English and Hebrew) "שלום hello". its encode is Unicode. when I do "readfile" and send a header:

Content-type: text/plain;charset=utf-8

the Hebrew chars disappear. The weird part is that when I open the file again I see the Hebrew chars(Unicode supports Hebrew), and UTF-8 extends Unicode so theoretically I don't need to do any conversion. What should I do?

1条回答
时光不老,我们不散
2楼-- · 2019-08-30 18:31

The line "its encod[ing] is Unicode" makes no sense. There are many ways Unicode can be represented in a text file (UTF-8 is one of them).

If you mean - it was saved in Notepad as Unicode, that means the file is in UTF-16LE. Send the respective header:

Content-type: text/plain;charset=utf-16

And it'll work.

To answer the question in the title, normally the iconv() function is used in PHP to convert between encodings. A word of caution though - it might not be available on the Web server in a hosted environment.

查看更多
登录 后发表回答