Nov
29th
Mon
29th
How to build a BOM in PHP
Byte order marks are the secret bane of many a PHP coder, and usually you want to remove them from documents that you are about to process.
But if you are sending UTF-8 CSV data to Excel, then you need to add one, otherwise your unicode is gonna be gibberish.
// send BOM
echo pack( 'C3', 0xef, 0xbb, 0xbf );
// then immediately send output
print $csv;
Note that this doesn’t work with Excel 2004 (Mac). As far as I can tell, there is no way to force Excel 2004 to read unicode CSV files. I mean, who would want to? :-p