用PHP中的BOM将字符串编码为UTF-8 [英] Encoding a string as UTF-8 with BOM in PHP

查看:177
本文介绍了用PHP中的BOM将字符串编码为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用utf8_encode时如何强制PHP添加BOM?

how can I force PHP to add the BOM when using utf8_encode ?

这就是我想要做的:

$zip->addFromString($filename, utf8_encode($xml));

不幸的是(对我来说),结果开头没有BOM标记.

Unfortunately (for me), the result will not have the BOM mark at the beginning.

推荐答案

您是否尝试过自己添加一个?

Have you tried adding one yourself?

UTF-8 BOM 似乎是0xEF 0xBB 0xBF,因此您可以附加转换为UTF-8后,将其保存到您的字符串.

The UTF-8 BOM seems to be 0xEF 0xBB 0xBF, so you can attach it to your string after conversion to UTF-8.

$utf8_with_bom = chr(239) . chr(187) . chr(191) . $utf8_string;

但是要当心. utf8_encode 需要一个ISO-8859-1字符串.如果您使用的是XML,请确保XML尚未 UTF-8编码.文档中的注释表明该功能以多种有趣的方式破坏了,因此除非您知道需要它,否则不要乱扔它.

Watch out, though. utf8_encode wants an ISO-8859-1 string. If you're working with XML, make sure that the XML isn't already UTF-8 encoded. The comments on the documentation suggest that the function is broken in a variety of fun ways, so you shouldn't throw it around unless you know that you need it.

请记住,PHP字符串只是愚蠢的,不知道字节.它们没有附加的字符集,因此,如果字符串中的数据已经是UTF-8,则无需运行转换.

Remember, PHP strings are simply dumb, unknowing bytes. They don't have a character set attached to them, so if the data in the string is already UTF-8, you don't need to run the conversion.

此外,链接的Wikipedia文章说:

Also, the linked Wikipedia article says this:

虽然Unicode标准允许使用UTF-8中的BOM,但它不需要或不推荐它.字节顺序在UTF-8中没有任何意义,因此BOM表只能将文本流或文件标识为UTF-8,或者是从具有BOM表的另一种格式转换而来.

While Unicode standard allows BOM in UTF-8, it does not require or recommend it. Byte order has no meaning in UTF-8 so a BOM only serves to identify a text stream or file as UTF-8 or that it was converted from another format that has a BOM.

开始时,您可能不必费心BOM轻敲.

You probably don't need to bother with the BOM tapdance to begin with.

这篇关于用PHP中的BOM将字符串编码为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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