php导出数据与重音到csv

时间:2015-02-05 13:16:25

标签: php csv character-encoding

我有以下代码创建一个csv文件:

$data=array();
array_push($data,"Société"); //word with french accent


header('Content-Type:text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="test.csv"');


$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
    $val = explode(",", $line);
    fputcsv($fp, array_map('utf8_decode',array_values($val)), ',', '"');
}
fclose($fp);

但是,当我打开csv文件时,字符é会被问号?取代。

如何解决此问题?

3 个答案:

答案 0 :(得分:1)

您是否尝试在array_map调用中删除“utf8_decode”参数?

如果您在UTF-8字符串上调用“utf8_decode”函数,它将在 LATIN-1编码(不支持重音符号)下返回。

请参阅utf8_decode的PHP文档> http://php.net/manual/en/function.utf8-decode.php

Bon勇气。

答案 1 :(得分:0)

前一段时间我遇到了类似的问题,我通过在任何csv行之前添加3字节UTF8表示来修复它:

header('Content-Type: application/x-download');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="export-'.time().'.csv"');

echo chr(0xEF);
echo chr(0xBB);
echo chr(0xBF);

答案 2 :(得分:0)

我在字符串中使用了UTF-8个字符。我换了

array_push($data,"Société"); //word with french accent

array_push($data,"Soci\xc3\xa9t\xc3\xa9"); //word with french accent