PHP Convert Windows-1251 to UTF 8

2020-03-12 04:38发布

I have a small html code and I need to convert it to UTF-8.
I use this iconv("windows-1251", "utf-8", $html);

All text converts correctly, but if text for example in tag <i>...</i>, then it don't convert text and I see somethig like this Показать мн

5条回答
smile是对你的礼貌
2楼-- · 2020-03-12 05:11

You know, message like Показать мн you see if encoding for page is windows-1251, but text encoded in utf-8.
I saw this problem in one of my project, so just change change encoding for page in utf-8 and this text will shown correctly.

Let me take you some examples:
if page in utf-8, but text in windows-1251 you wil see something like this:
???? ?? ?????? ??? ????? ??? ??????? ?? ????? ???? ??? ?????

if page in windows-1251, but text in utf-8 you see this:
"Мобильные телефоны";"Apple iPhone 4

查看更多
时光不老,我们不散
3楼-- · 2020-03-12 05:16

If you have access to the Multibye package, you can try it. See the PHP page here: http://www.php.net/manual/en/function.mb-convert-encoding.php

$html_utf8 = mb_convert_encoding($html, "utf-8", "windows-1251");
查看更多
forever°为你锁心
4楼-- · 2020-03-12 05:20

I always use manual convertation (character-by-character), like this:

$input= 'Обращение РљР°С';



$s= str_replace('С?','fgr43443443',$input);
$s= mb_convert_encoding($s, "windows-1251", "utf-8");
$s= str_replace('fgr43443443','ш',$s);


echo $s;

p.s. dont forget, the .php file encoding should be UTF8. also, in the head of HTML,insert standard declaration for UTF8

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
查看更多
何必那么认真
5楼-- · 2020-03-12 05:23

Most of the solutions lack conversion to single-byte encoding. I use mb_convert_encoding($string,'windows-1251') to convert from UTF-8 in my case.

function ru2Lat($string)
{
$rus = array('ё','ж','ц','ч','ш','щ','ю','я','Ё','Ж','Ц','Ч','Ш','Щ','Ю','Я');
$lat = array('yo','zh','tc','ch','sh','sh','yu','ya','YO','ZH','TC','CH','SH','SH','YU','YA');
$string = str_replace($rus,$lat,$string);
$string = strtr($string,
     "АБВГДЕЗИЙКЛМНОПРСТУФХЪЫЬЭабвгдезийклмнопрстуфхъыьэ",
     "ABVGDEZIJKLMNOPRSTUFH_I_Eabvgdezijklmnoprstufh'i'e");

return($string);
}

function transliterate($string){
    if (!is_string($string)) return $string;
    return ru2lat(mb_convert_encoding($string,'windows-1251'));
}

function transliterate_array($a){

$c = array_map(transliterate,$a);
             return $c;

}
查看更多
一夜七次
6楼-- · 2020-03-12 05:32

try this, works for me!

$result = str_replace ('€', '€' , $result);
查看更多
登录 后发表回答