如何检测 PHP 中的 MacRoman 编码?

Ken*_*and 5 php encoding mac-roman php-7.2

PHPmb_detect_encoding()不理解MacRoman编码。我的应用程序允许用户以 csv 格式上传数据,我需要将其转换为 utf8,因为用户不精通技术。我永远无法让他们所有人都了解如何做到这一点并控制他们的编码。

\n\n

这就是我正在做的事情:

\n\n
$encoding_detection_order = array(\'UTF-8\', \'UTF-7\', \'ASCII\', \'ISO-8859-1\', \'EUC-JP\', \'SJIS\', \'eucJP-win\', \'SJIS-win\', \'JIS\', \'ISO-2022-JP\', );\n\n$encoding = mb_detect_encoding($value, $detection_order, true);\n\n$converted_value = iconv($encoding, \'UTF-8//TRANSLIT\', $value);\n
Run Code Online (Sandbox Code Playgroud)\n\n

这对于大多数情况都很有用,但如果我的用户使用 Mac 并且他们以MacRoman编码方式保存 CSV,那么上面的代码通常会错误地检测到文本,从而ISO-8859-1导致iconv()产生错误的输出。

\n\n

例如,重音 e in 的Jaim\xc3\xa9十六进制值为0x8ein MacRoman。在 中ISO-8859-10x8e字符是\xc5\xbd,所以当我将其转换为 utf8 时,我只是得到\xc5\xbd了应该得到的 utf8 版本\xc3\xa9

\n\n

在此输入图像描述

\n\n

我需要能够动态地区分MacRoman其他编码,以便正确转换它。

\n