PHP Ansi to UTF-8

Paris picture Paris · Jun 26, 2011 · Viewed 14.8k times · Source

I'm trying to create a script in PHP for converting some files to UTF-8. I have a file in Greek, where Notepad++ indicates that it ahs "ANSI" encoding. When I upload it to the server, it detects it's encoding as UTF-8 (something wrinf i think). Then when I convert it's contents to UTF-8 with utf8_encode () and download the new file, the characters are messed up. I tried to remove the BOM with PHP and the result was the same. I tried to remove the BOM with PHP without converting the file to UTF-8 but the file remained in ANSI encoding, without messed up characters. How can I fix that?

Answer

OZ_ picture OZ_ · Jun 26, 2011

Remove BOM, then do:

$file = file_get_contents('file.php');
$file = iconv('greek-charset','UTF-8', $file);
file_put_contents('file.php', $file);
//ta-da!

Change greek-charset to correct name of charset (maybe Windows-1253).