在php中使用fputcsv将utf-8字符写入文件

2021-12-27 00:00:00 encoding character-encoding php

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

I want to try write Persian character in CSV file in PHP, I am using fputcsv function but how can write UTF-8 character to CSV file with fputcsv?

我的部分代码:

$df = fopen($filepath, 'w');
fputcsv($df, array($coupon->code, $discount->label));

推荐答案

试试这个:

$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)); 写入文件头以进行正确编码.

the line fprintf($df, chr(0xEF).chr(0xBB).chr(0xBF)); writes file header for correct encoding.

相关文章