I’m working on a PHP script that creates a .csv file from some data.
Unfortunately, in Excel Umlauts are not displayed properly: Löl
becomes Löl
(this is just in Excel, in Apple’s Numbers, Atom and Textedit everything looks fine).
I have tried hundreds of functions to try and get the encoding right (see the commented functions below). Could someone please tell me what I’m doing wrong?
JavaScript
x
$rows = json_decode('[["Löl"]]');
foreach($rows as $key_row => $row) {
foreach($row as $key_cell => $cell) {
// $rows[$key_row][$key_cell] = utf8_decode($cell);
// $rows[$key_row][$key_cell] = iconv('UTF-8', 'Windows-1252', $cell);
// $rows[$key_row][$key_cell] = mb_convert_encoding($cell, 'UTF-16LE', 'UTF-8');
// iconv('UTF-8', 'Windows-1252', $rows[$key_row][$key_cell]);
// mb_convert_encoding($rows[$key_row][$key_cell], 'UTF-16LE', 'UTF-8');
}
}
$temp = fopen('php://memory', 'w');
foreach($rows as $row) {
fputcsv($temp, $row, ';');
}
fseek($temp, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="test.csv";');
fpassthru($temp);
Advertisement
Answer
The following conversion to UTF-8 BOM
worked:
JavaScript
foreach($rows as $key_row => $row) {
foreach($row as $key_cell => $cell) {
$rows[$key_row][$key_cell] = chr(239) . chr(187) . chr(191) . $cell;
}
}
Thanks for everybody who chimed in 🙂