Ketan Ketan - 1 month ago 18
PHP Question

php export data with accents into csv

I have the following code that creates a csv file:

$data=array();
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)), ',', '"');
}
fclose($fp);


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

Any help how to resolve this?

Answer

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

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

with

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