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));
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.
You also have to take consideration in excel perspective. Make sure the excel already encoded in UTF-8
Good solution. Thanks Hardy!
Could you please explain what the three characters are and what is their purpose in fixing the UTF8 problem?
@AhmadBaktashHayeri those bytes make up the Byte Order mark (BOM) for UTF-8
My hero, saved my day :-*