How to write a text file with ANSI encoding (Western Windows-1252) in PHP

The fwrite function of PHP or file_put_content doesn't care about metadata of the file, it just writes the given data to the storage and that's it. This leads to a simply conjecture, the charset of the text file is defined by its data, so you need to convert the data that you will write into the file to a specific format.

In some enterprises, this process is necessary as the software of other big companies is out of date and doesn't operate well with the UTF-8 default encoding, so you will need to change obligatorily the encoding of your generated files to the named "ANSI" codification. The term "ANSI" when applied to Microsoft's 8-bit code pages is a misnomer. They were based on drafts submitted for ANSI standardization, but ANSI itself never standardized them. Windows-1252 (the code page most commonly referred to as "ANSI") is similar to ISO 8859-1 (Latin-1), except that Windows-1252 has printable characters in the range 0x80..0x9F, where ISO 8859-1 has control characters in that range. Unicode also has control characters in that range.

In PHP, you can achieve such thing using the iconv function, trying to detect the encoding of your data (usually UTF-8) and convert it into the new format namely Windows-1252 (CP1252):

<?php

// Store your original text in some variable
$data = "Los señores del pueblo de alli comen sopa con cucharas.";

// Change the encoding of the file using iconv
$string_encoded = iconv( mb_detect_encoding( $data ), 'Windows-1252//TRANSLIT', $data );

// Write File
$file = fopen("my_text_file.txt", "w+");
fwrite($file, $string_encoded);
fclose($file);

The output file will be recognized automatically by your favorite text editor with the Windows-1252 encoding:

Windows 1252 Text PHP

And that's it, normally. If the content of your files doesn't have special characters, the content may be recognized by the text editor as UTF-8, so your only option will be to use a CLI tool to convert the encoding of the file (read next paragraph).

If file still without correct encoding

If after using plain PHP to write the content manually on your file doesn't use the desired encoding in the content of the file, you may need to use instead the system-level way to set the encoding of the file.

The preferred and easiest way to do it using the CLI is with iconv. This CLI tool converts the encoding of characters in inputfile from one coded character set to another. The result is written to standard output unless otherwise specified by the --output option or by a simply output redirect with the cli, for example:

iconv -t CP1252 -f UTF-8 "input_file.txt" > "encoded_output_file.txt"

You can use the exec function of PHP to run the iconv program with the mentioned arguments, available on every UNIX based OS or in Windows (using Cygwin).

As mentioned, most of the text editors that use the automatic encoding detector decide which encoding to use according to the content, so normally although the strings stored in the file with PHP that have the Windows-1252 encoding (CP1252), the UTF-8 mode will be used. This means that the detection of the encoding based on the content is based just on heuristic, so doesn't guarantee that the encoding used to open the file is correct.

However, the content doesn't lie, so if  you try to open the content of a Windows-1252 encoded file with the UTF-8 encoding, you will see weird characters in the text editor:

Windows1252 (CP1252) text opened with UTF-8

However if the text editor reads the content of the file with the Windows-1252 (CP1252) encoding, it will be read correctly:

Windows 1252 (CP1252) opened with correct encoding

Happy coding !

Become a more social person