macos 如何使用 PHP 将“西方(Mac OS Roman)”格式的文本转换为 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4722864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert "Western (Mac OS Roman)" formatted text to UTF-8 with PHP?
提问by Edward Tanguay
I have files being exported by Excel for Mac 2011 VBA in Western (Mac OS Roman)as shown here:
我有 Excel for Mac 2011 VBA 在西方(Mac OS Roman)中导出的文件,如下所示:
I haven't been successful in getting Excel for Mac VBA to export directly to UTF-8so I want to convert these files with PHP before I save them to MySQL, I am using this command:
我没有成功地将 Excel for Mac VBA 直接导出为 UTF-8,所以我想在将这些文件保存到 MySQL 之前用 PHP 转换它们,我正在使用以下命令:
$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"
I found this list of valid encoding formatsfrom 2008, but none of them seem to represent Western (Mac OS Roman)
.
我找到了这份2008 年的有效编码格式列表,但似乎没有一个代表Western (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
What format do I need to use to convert "Western (Mac OS Roman) to UTF-8?
我需要使用什么格式将“西方(Mac OS Roman)”转换为 UTF-8?
回答by rik
The mb-functions can't handle "macintosh" which is the IANA defined name for Mac Roman. You have to use iconv
.
mb 函数无法处理“macintosh”,这是 IANA 为 Mac Roman 定义的名称。你必须使用iconv
.
$line = iconv('macintosh', 'UTF-8', $line);