【问题标题】:issue converting to utf-8 csv file made with excel on mac在 mac 上转换为使用 excel 制作的 utf-8 csv 文件的问题
【发布时间】:2015-03-28 04:39:13
【问题描述】:

我是一个编码新手。 我有一个允许用户上传 csv 文件的 php 文件。

我的问题是,当使用 excel for mac 创建文件时,如果文件包含重音字母等 utf-8 字符,我的代码将无法正常工作。基本上它会忽略重音字符。

仅当使用Comma separated values 选项保存文件时才会出现此问题。

在所有其他情况下,例如在 windows 中创建文件或使用 open office 甚至是 mac 上的 excel,但将它们保存为“windows”文件不会导致任何问题。

mb_detect_encoding 为导致问题的文件返回 false。

代码如下:

// say there is the word Nestlé in the file
$content = file_get_contents(addslashes($file_name));

var_dump(mb_detect_encoding($content)); // print false


$data  = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
            //$data  = utf8_encode($content);  //doesn't work

var_dump($data); // print Nestl

ini_set('auto_detect_line_endings',TRUE);

// more code here we don't need at the moment

这个问题给了我一些暗示:file_get_contents() Breaks Up UTF-8 Characters

关于如何解决这个问题的任何帮助或想法?提前谢谢你

这是 Anthony 发布响应后的新代码

$content = file_get_contents(addslashes($file_name));
// i have no control on how the file is generated so i need to to the replace in the code
$content = str_replace(",", "\t",  $content);
var_dump($content);
$data  = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));


$data =  mb_convert_encoding($data, 'UTF-16LE', 'UTF-8');
$data = chr(255) . chr(254) . $data;

var_dump($data); // this still print funny characters not the accented letter

我做错了吗?

【问题讨论】:

  • 这是 osx 上 excel 的已知问题,缺少对 csv 文件的 Unicode 支持。有一个解决方法,我会发布一些。你没有做错,它是 excel
  • @Anthony 这对我来说是个不错的生日礼物 :)

标签: php excel csv


【解决方案1】:

好的,谢谢 Anthony,下面是修复它的行:

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

所以我的最终代码将如下所示:

enter code here

$content = file_get_contents(addslashes($file_name));

var_dump(mb_detect_encoding($content));
// need to do this for an issue specific to Excel and more common on Excel for Mac
// using excel on mac if the file is saved as csv using the Comma separated values option we need to use iconv and not mb_convert_encoding
// we use mb_detect_encoding because the content of such file returns a false value
if(!mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)){
     //$data  = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', 'macintosh', true));

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


 } 
    // deal with known encoding types
 else{
         $data  = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
 }

【讨论】:

    【解决方案2】:

    这是 Excel 特有的问题,在 Excel for Mac 上更为常见,其中 UTF-8 多字节字符无法正确显示。您可以使用其他电子表格查看器进行确认,例如 Google 表格。

    解决方法是:

    1. 使用制表符 (\t) 而不是逗号作为分隔符(别担心,技术上它仍然是 CSV)。

    2. 编码为utf-8后,将整个csv字符串转为UTF-16LE:

      mb_convert_encoding($csv_content, 'UTF-16LE', 'UTF-8');

    3. 用 little-endian 字节顺序标记 (LE BOM) 为 csv 字符串添加前缀:

      $csv_content = chr(255) . chr(254) . $csv_content;

    应该这样做。

    【讨论】:

    • 查看我的编辑,我无法让您的解决方法起作用,您能发现我的代码中有什么问题吗?也许这是我的线路,我转换为 utf-8 不起作用
    • 我实际上把你的问题倒过来了。我的解决方案是输出一个适用于 Mac 上的 Excel 的 csv,而不是用于导入。给我几分钟看看解决方案是否反过来。
    • 嗯,我使用您的示例 Nestlé 创建了一个测试 csv。看起来这是使用 Mac OS Roman 编码的,需要 iconv 转换为 utf-8:stackoverflow.com/questions/4722864/…
    • 我的猜测是 Mac OS 罗马代码块中的字符(如 é)使用该字符集进行编码,但如果字符不在该集中(如日文字符)可能使用 UTF 编码-16 或其他一些编码。所以你应该使用mb_detect_encoding,如果它返回false,那么假设它是Mac OS Roman。
    • 这就是我刚刚完成的!非常感谢您帮助我!
    猜你喜欢
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 2016-12-09
    相关资源
    最近更新 更多