O C*_*nor 5 php csv character character-encoding
以下是我从浏览器创建和下载CSV文件的代码段.
$input_array[] = ['????', '????',];
$input_array[] = ['2015-09-30', 'INV-00001',];
/** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
$f = fopen('php://memory', 'w');
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($f, $line, ',');
}
/** rewrind the "file" with the csv lines **/
fseek($f, 0);
/** modify header to be downloadable csv file **/
header('Content-Encoding: UTF-8');
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachement; filename="my_csv_file.csv";');
/** Send file to browser for download */
fpassthru($f);
die();
Run Code Online (Sandbox Code Playgroud)
当我打开创建/下载的CSV文件时,日文字符成为奇怪的字符.我的代码段中还有什么不正确?如何在创建CSV文件时保留日文字符?
echo
如果要强制任何程序将文件解释为UTF-8编码,则只需在写第一行之前直接添加一个带简单顺序的字节顺序标记:
$input_array[] = ['????', '????',];
$input_array[] = ['2015-09-30', 'INV-00001',];
echo "\xEF\xBB\xBF";/// Byte Order Mark HERE!!!!
/** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
$f = fopen('php://memory', 'w');
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($f, $line, ',');
}
/** rewrind the "file" with the csv lines **/
fseek($f, 0);
/** modify header to be downloadable csv file **/
header('Content-Encoding: UTF-8');
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachement; filename="my_csv_file.csv";');
/** Send file to browser for download */
fpassthru($f);
die();
Run Code Online (Sandbox Code Playgroud)
或者,Unicode Character set
当您使用Excel或Open Calc导入时选择它,否则直接使用notepad / textedit / etc打开它没有问题