German characters not showing properly on csv file using php
I am trying to generate a CSV file, which gets created perfectly except for the german characters ä,ë,ï,etc I just get the information to a $data variable and tried to use:
utf8_encode($data);
but I get weird text, h开发者_运维知识库ere are my headers for the file:
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"file1.csv\"");
I have tried using also iconv(); but no luck thus far.
EDIT
just noticed that my $data is already utf-8 so I removed the utf8_encode(); still, when I try to print it I get ?
and changed the header to:
header("Content-Type: text/plain; charset=utf-8");
Use:
header("Content-Type: text/plain; charset=utf-8");
And make sure your $data
isn't already utf-8!
Don't know, if this may cause the error: csv is an ascii-text file. You should use text/csv
or at least text/plain
. At all every text/*
-mime-type is more useful, then application/octet-stream
, because this defines binary data, what csv-files are obviously not.
Then if the file is already encoded in UTF-8, don't re-encode it
utf8_encode($data); // not useful, if $data is already utf-8
Next, add the charset to the content-type header
Content-Type: text/csv; charset=utf-8
(at all)
header("Content-Type: text/csv; charset=utf-8");
And last but not least: Make sure, that the application, with which you want to open the file, understand utf-8 and also uses it for the file.
精彩评论