Answer the question
In order to leave comments, you need to log in
How to generate correct CSV encoding using PHP?
I'm trying to create a csv file via php, I'm doing it as in the manual (I attached an example), but the problem is that the encoding always goes astray, everything is fine in notepad. I looked and tried a lot of things, but still nothing, I found a lot of functions fragmentarily, but it’s not clear what to change: the csv itself, the fields that I’m trying to put in there, or just Excel is stupid.
I'm new to php, only been doing this for a week. So I'm sorry for the stupid questions, but I really need help
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
?>
Answer the question
In order to leave comments, you need to log in
$fp = fopen('file.csv', 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
//.....
there was such an option on the project
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question