Php – Iconv convert from UTF-8 to Windows-1250 doesn’t work

character encodingencodingPHPutf-8

I've always had problems with iconv. Now I must convert string to Windows-1250 and this doesn't seems to work:

$string = "ľaľa ho papľuha, ogrcal mi krpce!";
echo $string . ' ( ' . mb_detect_encoding($string) . ' ) <br>';
$string_encoded = iconv( mb_detect_encoding( $string ), 'Windows-1250//TRANSLIT', $string );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';
$string_encoded = mb_convert_encoding( $string, 'Windows-1250' );
echo $string_encoded . ' ( ' . mb_detect_encoding($string_encoded) . ' ) <br>';

The three echos above output exactly this:

ľaľa ho papľuha, ogrcal mi krpce! ( UTF-8 )
�a�a ho pap�uha, ogrcal mi krpce! ( ) 
mb_convert_encoding() Unknown encoding &quot;Windows-1250&quot; ( ASCII )

Since I've always seen this diamond question marks I wonder if this PHP function works at all. How can I convert UTF-8 to Windows-1250?

  • The file was saved in notepad+ in UTF-8
  • Also I've tried header('Content-Type: text/html; charset=windows-1250'); and setLocale()

Best Answer

I have experienced a similar issue. While reading CSV file, word "Česká republika" was read as "Èeská republika".

This solved it for me:

iconv( "Windows-1250", "UTF-8", ($string));