ononononon
ononononon

Reputation: 1093

Iconv convert from UTF-8 to Windows-1250 doesn't work

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?

Upvotes: 12

Views: 34984

Answers (4)

Pavel Vydra
Pavel Vydra

Reputation: 11

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

Upvotes: 1

Matej Podstrelenec
Matej Podstrelenec

Reputation: 111

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));

Upvotes: 11

Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6678

Is old post but you can convert UTF-8 to Windows-1252 and you will have same effect:

$str = "ľaľa ho papľuha, ogrcal mi krpce!"
$str = mb_convert_encoding( $str, "Windows-1252", "UTF-8" );

but if you realy need Windows-1250 you can use THIS SOLUTION and adapt to your need.

Upvotes: 2

deceze
deceze

Reputation: 522626

The � character is an indication that your text is being interpreted as UTF-8, but at this point an invalid byte sequence was encountered. Meaning, you're not serving UTF-8, yet the client is reading it as UTF-8. Which would imply that iconv is working just fine and whoever is reading the result just didn't get the message that it should be interpreting it as Windows-1250.

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App.

Upvotes: 6

Related Questions