PHP: using UTF8 for CSV creation

There is a mobile optimized version of this page, view AMP Version.

If your generating a CSV with PHP you may have stumbled upon the same problem as me where any special character such as £ becomes ASCII in your CSV, this is because the encoding of your CSV is wrong and you will need to output your CSV in UTF8 to be able to use special ASCII characters in your document.

Using my example below you can encode UTF8 your CSV file, you will notice a header with “Content-Encoding: UTF-8” and also in the “Content-Type” header you will see an extra parameter “; charset=UTF-8”. You will also notice the weird echo “\xEF\xBB\xBF” – this is the BOM for the UTF-8 encoding, if your not sure what this is don't worry, it will not display in your document.

Please Note! UTF-8 is not supported by Macintosh version of Excel, so you will see both the BOM and the encode characters instead of the correct character – there is no fix for this and you will have to provide a version of the CSV without special characters if you need to avoid it.

So here is my example code, this will generate a CSV file using PHP’s CSV functions and will add UTF-8 support:


header('Content-Encoding: UTF-8');
header("Content-type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=processed_devices.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo "\xEF\xBB\xBF";
$fp = fopen('php://output', 'w');

$list = array(
		array('id', 'name', 'value'),
		array('1', 'brent', '12'),
		array('1', 'matt', '45'),
		array('1', 'gaz', '34'),
		array('1', 'tom', '76')

foreach ($list as $fields) {
	fputcsv($fp, $fields);


Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips

  • Mike

    Awesome thanks for sharing