gpt4 book ai didi

php - 在 PHP 中使用 BOM 将字符串编码为 UTF-8

转载 作者:IT王子 更新时间:2023-10-28 23:48:57 25 4
gpt4 key购买 nike

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

这是我想要做的:

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

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

最佳答案

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

UTF-8 BOM似乎是 0xEF 0xBB 0xBF,因此您可以将它附加到您的字符串 after 转换为 UTF-8。

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

不过要小心。 utf8_encode想要一个 ISO-8859-1 字符串。如果您使用的是 XML,请确保 XML 不是已经 UTF-8 编码的。文档中的评论表明该功能以各种有趣的方式被破坏,因此除非您知道您需要它,否则您不应该随意使用它。

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

另外,链接的维基百科文章是这样说的:

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 踢踏舞操心。

关于php - 在 PHP 中使用 BOM 将字符串编码为 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5601904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com