如何使用PHP将“Western(Mac OS Roman)”格式的文本转换为UTF-8?

8

我有一些由Excel for Mac 2011 VBA导出的文件,其字符集为Western (Mac OS Roman),如下图所示:

alt text

我尝试过直接将Excel for Mac VBA导出为UTF-8格式,但没有成功。因此,我想在将它们保存到MySQL之前使用PHP将这些文件转换为UTF-8格式。我正在使用以下命令:

$dataset[$k] = mb_convert_encoding($line, 'ASCII', 'UTF-8'); //not correctly converted
$dataset[$k] = mb_convert_encoding($line, 'ISO-8859-8', 'UTF-8'); //not correctly converted
$dataset[$k] = mb_convert_encoding($line, 'macintosh', 'UTF-8'); //unrecognized name
$dataset[$k] = mb_convert_encoding($line, 'Windows-1251', 'UTF-8'); //changes "schön" to "schљn"
$dataset[$k] = mb_convert_encoding($line, 'Windows-1252', 'UTF-8'); //changes "schön" to "schšn"

我发现了这个有效编码格式的列表,但是它们中没有一个代表西方(Mac OS Roman)

* UCS-4
* UCS-4BE
* UCS-4LE
* UCS-2
* UCS-2BE
* UCS-2LE
* UTF-32
* UTF-32BE
* UTF-32LE
* UTF-16
* UTF-16BE
* UTF-16LE
* UTF-7
* UTF7-IMAP
* UTF-8
* ASCII
* EUC-JP
* SJIS
* eucJP-win
* SJIS-win
* ISO-2022-JP
* JIS
* ISO-8859-1
* ISO-8859-2
* ISO-8859-3
* ISO-8859-4
* ISO-8859-5
* ISO-8859-6
* ISO-8859-7
* ISO-8859-8
* ISO-8859-9
* ISO-8859-10
* ISO-8859-13
* ISO-8859-14
* ISO-8859-15
* byte2be
* byte2le
* byte4be
* byte4le
* BASE64
* HTML-ENTITIES
* 7bit
* 8bit
* EUC-CN
* CP936
* HZ
* EUC-TW
* CP950
* BIG-5
* EUC-KR
* UHC (CP949)
* ISO-2022-KR
* Windows-1251 (CP1251)
* Windows-1252 (CP1252)
* CP866 (IBM866)
* KOI8-R

我需要使用什么格式将“Western (Mac OS Roman)”转换为UTF-8?


3
你尝试过使用 iconv 吗? - Gordon
C++代码/表格:https://dev59.com/WHTYa4cB1Zd3GeqPs0Yc#58022902 - M Katz
1个回答

22

mb函数无法处理“macintosh”,而“macintosh”是Mac Roman的IANA定义名称。您必须使用iconv

$line = iconv('macintosh', 'UTF-8', $line);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接