I am generating a CSV file with PHP and saving it on the hard disk (I do not want a download). The database charset is utf8_general_ci and there are chars in there like the degrees sign (°), which show up perfectly in PHPmyadmin. These chars need to be saved in the CSV file. Everything I do is in UTF-8, but the file gets saved with the encoding ISO-8859-1. If I start using the utf8_encode function, other chars show up (Â) when I convert it back to UTF-8 in excel or any other text editor. I do not know what is going wrong and I have spent hours to track it down, without success.
I have tried SET NAMES utf8 just to be sure it is UTF-8, but that just causes more strange characters and no UTF-8 encoding. Using MySQLi’s set_charset does the same. With the header as Content-type:text/html;charset=UTF-8 the CSV still does not have utf-8 as default encoding, nor with any of these combinations.
Some code:
header("Content-type:text/html;charset=UTF-8");
$rMysqli = new mysqli("localhost", "user", "pass", "database");
$rCSV = fopen("test_".time().".csv", 'w');
$aCSVdata = array($var1, $var2, $var3);
fputcsv($rCSV, $aCSVdata, ",", "\"");
fclose($rCSV);
That is pretty much all is happening. Just normal CSV data, only with special chars such as ° and Ø.
…long story of the comments short…
Microsoft Office is the worst when it comes to encodings. It notoriously sucks at it.
(I.e. everything was fine except Excel.)