Ketan Ketan - 3 months ago 46
PHP Question

php export data with accents into csv

I have the following code that creates a csv file:

array_push($data,"Société"); //word with french accent

header('Content-Type:text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="test.csv"');

$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, array_map('utf8_decode',array_values($val)), ',', '"');

However when I open the csv file, the character
is replaced by a question mark

Any help how to resolve this?


I used UTF-8 characters in the string. I replaced

array_push($data,"Société"); //word with french accent


array_push($data,"Soci\xc3\xa9t\xc3\xa9"); //word with french accent