Search code examples
phpexcelutf-8asciimb-convert-encoding

how does mb_convert_encoding work?


I had a trouble exporting UTF-8 data to Excel, but now it's ok, because I've found this:

Microsoft Excel mangles Diacritics in .csv files?

Look at this line:

echo chr(255) . chr(254) . mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8');

When I remove chr(255) . chr(254) at the beginning the Excel can't display UTF-8 data normally, so can't the browser.

It's not a problem of course, I would just like to know, why those chr(255) and chr(254) are essential.


Solution

  • It seems, that Excel requires the Byte Order Mark (BOM). The BOM is always at the beginning of a file and either FFFE or FEFF. It describes in which order the separate bytes of a multibyte-character appears (lets say "forward" or "backward"). Usually its strongly recommended to omit it, but it seems, that excel uses it to determine the file encoding.