Convert php - iconv from UTF-8 to Windows-1250 not working

Hi guys. I always had problems with iconv. Now I have to convert the string to Windows-1250, and this does not 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 echoes above display 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 always saw this diamond question, I wonder if this php function works at all. How to convert UTF-8 to Windows-1250?

  • File was saved in notepad ++ in UTF-8
  • I also tried the header ('Content-Type: text / html; charset = windows-1250'); and setLocale ()
+6
source share
3 answers

The is symbol is a sign that your text is interpreted as UTF-8, but at that moment an incorrect sequence of bytes was encountered. So you are not using UTF-8, but the client reads it as UTF-8. Which would imply that iconv works fine, and whoever reads the result simply did not receive a message saying that it should interpret it as a Windows-1250.

See What every programmer absolutely needs to know positively about the encodings and character sets for working with text and Working with Unicode Front To Back in a web application .

+3
source

Old mail, but you can convert UTF-8 to Windows-1252, and you will have the same effect:

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

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

+1
source

I had a similar problem. When reading a CSV file, the word "Česká republika" was read as "Èeská republika".

This solved it for me:

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

Source: https://habr.com/ru/post/982384/


All Articles