ŸŒcsv中的字符不会显示php

时间:2015-02-05 20:46:14

标签: php csv utf-8 character-encoding

我是编码新手所以请耐心等待。 我正在开发一个用户上传csv的系统,我需要做的是显示内容然后将其保存在数据库中。 (utf-8编码)

我被要求修复一些没有正确显示的法语字母字符的问题。我几乎解决了这个问题,我正在显示诸如

之类的字符

ÀàÂâÆÄäÇçÉéÈèÊêËëÎîÏïÔôœÖöÙùÛûÜüÿ

但标题Ÿ Œ中提到的两个内容尚未在网页上正确显示。

到目前为止,这是我的PHP代码:

// say in the csv we have "ÖüÜߟÀàÂ"
$content = file_get_contents(addslashes($file_name));
var_dump($content) // output: string(54) "���ߟ��� "
if(!mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)){
     $data = iconv('macintosh', 'UTF-8', $content);
} 
// deal with known encoding types
else if(mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true) == 'ISO-8859-1'){
    //$data  = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)); // does not work
    $data = iconv('ISO-8859-1', 'UTF-8', $content); //does not work

}else if(mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true) == 'UTF-8'){
    $data = $content
}
//if i print $data "Ÿ Œ " are not printed out... they got lost somewhere

       //do more stuff here

我正在处理的文件的编码类型为ISO-8859-1(当我打印出mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true)时,它会显示ISO-8859-1)。

有没有人知道如何处理这种特殊情况?

1 个答案:

答案 0 :(得分:3)

字符Ÿ和Œ在ISO-8859-1中无法表示。似乎传入的数据实际上是windows-1252(Windows Latin 1)编码,因为windows-1252在某些代码位置具有图形字符,包括Ÿ和Œ,这些位置是为ISO-8859-1中的控制字符保留的。

所以你应该将windows-1252添加到识别的编码列表中将识别的ISO-8859-1视为windows-1252,即使在ISO-8859-时使用iconv('windows-1252', 'UTF-8', $content) 1已被认可。错误标记为ISO-8859-1的Windows-1252数据非常常见。