Php: convert from cp1251 to utf8

I have a problem converting a string from cp1251 to utf8 ...

I need to get some names from the database, and these names are in cp1251 (I am not the one who created this database, so I can’t edit it, but I know for sure that these names are cp1251) ..

The name in the database is "Π…Π…, ΞΌΠ‚Π…ΞΌ, Π† † " " Β° ..." I convert it to utf8 using the iconv function as follows:

iconv ("UTF-8", "CP1251 // IGNORE", $ name)

and what I have as a result is "the Internet in numbers" (this is Russian), but the first two characters are not correct ... it should be "Internet in numbers" ...

So, the last thing I need to do is somehow change these two β€œ?” Characters. to the Russian letter "I" ... and I really don’t know how to do it ... I tried to use preg_replace, but it does not work ... or I do not use it correctly.

And I am sorry for the Russian letters, it is very difficult for me to explain what I need without showing them.

+4
source share
3 answers

The first letter is erroneous because one of the bytes required to store the UTF-8 encoding AND ( 0x98 , to be exact) is not used in CP1251 . If the database replaced byte 98 question mark, you must change it before using iconv :

 $name = str_replace("\xD0\x3F", "\xD0\x98", $name); echo iconv("UTF-8", "CP1251//IGNORE", $name); 
+3
source

use this:

 mb_convert_encoding($model->text, 'cp1252', 'utf8') 
+2
source

Try the following:

 function cp1251_to_utf8($s){ $c209 = chr(209); $c208 = chr(208); $c129 = chr(129); for($i=0; $i<strlen($s); $i++) { $c=ord($s[$i]); if ($c>=192 and $c<=239) $t.=$c208.chr($c-48); elseif ($c>239) $t.=$c209.chr($c-112); elseif ($c==184) $t.=$c209.$c209; elseif ($c==168) $t.=$c208.$c129; else $t.=$s[$i]; } return $t; } 
0
source

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


All Articles