PHP: Converting UTF-8 string to Ansi?

user1856596 picture user1856596 · Feb 18, 2013 · Viewed 61.1k times · Source

I build a csv string from values I have in my DB. The final string is stored in my $csv variable.

Now I offer this string for download, like this:

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=whatever.csv");
header("Pragma: no-cache");
header("Expires: 0");

echo $csv;

When I open this in Notepad++ for example, it says Ansi as UTF-8. How can I chnage that to Ansi only?

I tried:

$csv = iconv("ISO-8859-1", "WINDOWS-1252", $csv);

That did not change anything.

Thanks!

Solution: $csv = iconv("UTF-8", "WINDOWS-1252", $csv);

Answer

Fabian Schmengler picture Fabian Schmengler · Feb 18, 2013

Try:

$csv = iconv("UTF-8", "Windows-1252", $csv);

But you will eventually lose data because ANSI can only encode a small subset of UTF-8. If you don't have a very strong reason against it, serve your files UTF-8 encoded.