Warm tip: This article is reproduced from serverfault.com, please click

encoding-将utf-8字符写入php中的fputcsv文件

(encoding - write utf-8 characters to file with fputcsv in php)

发布于 2014-02-24 13:01:09

我想尝试在PHP的CSV文件中写波斯字符,我正在使用fputcsv函数,但是如何用UTF-8字符写到CSV文件fputcsv呢?

我的代码的一部分:

$df = fopen($filepath, 'w');
fputcsv($df, array($coupon->code, $discount->label));
Questioner
Yuseferi
Viewed
0
Hardy 2014-02-24 22:50:11

试试这个:

$df = fopen($filepath, 'w');
fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));
fputcsv($df, array($coupon->code, $discount->label));

该行将fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF));写入文件头以进行正确的编码。