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

write utf-8 characters to file with fputcsv in php

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

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?

Part of my code:

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

Try this:

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

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