Converting UTF-8 to ANSI for CSV Export
Posted: Fri Nov 28, 2008 6:37 am
Hi everyone
I'm converting a Filemaker database into an intranet PHP/MySQL system.
With the new php/MySQL it's all set to use utf-8 everywhere, database tables (utf8_general_ci), html output, mysql collations, and mysql connection collations.
So that's all great... Apart from when I want to export a CSV to read in Excel
When I'm exporting the CSV using PHP, this is what I'm doing:
And this almost works. However I'm getting stuck on certain eastern european characters... at the moment the CSV output stalls on a Z with an inverted circumflex hat thing on top.
When I view the CSV with a text editor (Notepad++), the CSV output just stops just before that character.
However when I export the same data from Filemaker, the CSV output works fine and opening up the file in Excel that character displays perfectly as well.
So excel seems to be capable of displaying that character, but I just need to get PHP to output past that point.
When I open both the output CSV files in Notepad++, they both show Dos\Windows and ANSI as the format.
Am I using the correct destination charset in my iconv function?
Or should that be a windows one or CP12... or whatever it is?
I understand that converting from UTF-8 to anything else is usually a lossy process when it comes to these characters.
Is there anything I can do to get PHP outputting past that point?
And any way to minimise the potential loss in char information?
Thanks, B
I'm converting a Filemaker database into an intranet PHP/MySQL system.
With the new php/MySQL it's all set to use utf-8 everywhere, database tables (utf8_general_ci), html output, mysql collations, and mysql connection collations.
So that's all great... Apart from when I want to export a CSV to read in Excel
When I'm exporting the CSV using PHP, this is what I'm doing:
Code: Select all
header('Content-type: text/csv; charset=ISO-8859-1');
header('Content-Disposition: attachment; filename="MailingData.csv"');
echo iconv('UTF-8', 'ISO-8859-1', $var);When I view the CSV with a text editor (Notepad++), the CSV output just stops just before that character.
However when I export the same data from Filemaker, the CSV output works fine and opening up the file in Excel that character displays perfectly as well.
So excel seems to be capable of displaying that character, but I just need to get PHP to output past that point.
When I open both the output CSV files in Notepad++, they both show Dos\Windows and ANSI as the format.
Am I using the correct destination charset in my iconv function?
Or should that be a windows one or CP12... or whatever it is?
I understand that converting from UTF-8 to anything else is usually a lossy process when it comes to these characters.
Is there anything I can do to get PHP outputting past that point?
And any way to minimise the potential loss in char information?
Thanks, B