如何在PHP中从Unicode转换为UTF-8? [英] how to convert from Unicode to UTF-8 in PHP?

查看:205
本文介绍了如何在PHP中从Unicode转换为UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存了名为"File1.txt"的文件,其内容(英语和希伯来语)为שלוםhello". 它的编码是Unicode. 当我执行"readfile"并发送标头时:

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

希伯来字符消失了. 奇怪的是,当我再次打开文件时,我看到希伯来字符(Unicode支持希伯来语),而UTF-8扩展了Unicode,因此从理论上讲我不需要进行任何转换. 我该怎么办?

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?

推荐答案

其编码"是Unicode行没有意义.可以在文本文件中表示Unicode的方法有很多(UTF-8是其中一种).

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).

如果您的意思是-它以Unicode格式保存在记事本中,则意味着该文件位于UTF-16LE中.发送相应的标头:

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

它将起作用.

要回答标题中的问题,通常在PHP中使用iconv()函数在编码之间进行转换.不过请注意-在托管环境中的Web服务器上可能不可用.

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.

这篇关于如何在PHP中从Unicode转换为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆