Ketan Ketan - 1 year ago 120
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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download