Excel won't handle UTF-8 in CSV? Force a different encoding in PHP

Dec 14, 2010 php programming
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

When generating a CSV file with PHP in the UTF-8 encoding, Microsoft Excel freaks out. It just doesn’t show the proper encoding. Thank you Microsoft! However, I did find a way to handle this encoding and make it compatible for Windows/Microsoft Excel.

Instead of outputing with encoding of UTF-8, change it on the fly to Windows-1251. This can be accomplished with mb_convert_encoding(). Check it out:

$csvOutput .= mb_convert_encoding(implode(',', $value), 'Windows-1251', 'UTF-8');
Go to All Posts