PHP: Convert UTF-8 string to Ansi?

I am building a csv string from the values ​​that I have in my database. The final line is stored in my $ csv variable.

Now I suggest this line for loading, for example:

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 is UTF-8. How can I use this only for Ansi?

I tried:

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

This has not changed anything.

Thanks!

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

+6
source share
3 answers

Try:

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

But you will end up losing data because ANSI can only encode a small subset of UTF-8. Unless you have a very strong reason, file your files encoded with UTF-8.

+18
source

Since there is a misunderstanding in your question regarding ISO-8859-1, Windows-1252, and ANSI, it is important to note that:

The so-called Windows character set (WinLatin1 or the Windows 1252 code page, to be precise) uses some of these positions for printed characters. Therefore, the Windows character set is NOT identical to ISO 8859-1. The Windows character set is often called the "ANSI character set", but this is SERIOUSLY WRONG. It has NOT been approved by ANSI.

Historical background: Microsoft bases the design of the kit on a draft for the ANSI standard. Microsoft’s glossary explicitly acknowledges this .

A few more resources: here and here .

So just FYI for other people who fall into this question.

Here is MS exact explanation about this:

The term ANSI, used to refer to Windows code pages, is a historical reference, but is currently incorrect, which continues to persist in the Windows community. The source of this is the fact that the Windows 1252 code page was originally based on the ANSI project, which became the standard of the International Organization for Standardization (ISO) 8859-1. "ANSI applications" are usually links to applications not related to Unicode or code page.

+5
source

To avoid data loss when converting special characters:

 setlocale(LC_CTYPE, "fr_FR.UTF-8"); //set your own locale $csv = iconv("UTF-8", "WINDOWS-1252//TRANSLIT//IGNORE", $csv); 
0
source

All Articles