Latest news:
Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif
While generating a CSV using PHP sometimes we stumbled upon one common problem
where any special character like £ becomes ASCII in generated CSV file,
the reason behind this is the encoding of generated CSV is wrong and
we need to output CSV in UTF8 format, so we will be able to use special ASCII characters in CSV file.
Using example below we can encode UTF8 generated CSV file,
We need to set up the header with "Content-Encoding: UTF-8" and also in the "Content-Type" header we need to set an extra parameter ";
charset=UTF-8".
There is also a weird echo "xEFxBBxBF" – this is the BOM for the UTF-8 encoding,
if your not sure what this is don't worry, it will not displayed in the document.
Remember this UTF-8 is not supported by MacOS 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.
Below are the example of code to generate a CSV file using PHP CSV functions with UTF-8 support:
header('Content-Encoding: UTF-8');
header("Content-type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment; filename=my_file_name.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "xEFxBBxBF";
$fp = fopen('php://output', 'w');
$list = array(
array('id', 'name', 'value'),
array('1', 'test', '12'),
array('1', 'morgan', '45'),
array('1', 'arthur', '34'),
array('1', 'tom', '76')
);
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
Views : 3485