问题描述
使用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:
开始时,您可能不必费心BOM轻敲.
You probably don't need to bother with the BOM tapdance to begin with.
这篇关于用PHP中的BOM将字符串编码为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!