PHP Convert Windows-1251 to UTF 8

人盡茶涼 提交于 2019-12-01 00:21:19

问题


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 Показать мн


回答1:


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



回答2:


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:


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">



回答4:


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;

}



回答5:


try this, works for me!

$result = str_replace ('€', '€' , $result);


来源:https://stackoverflow.com/questions/15706077/php-convert-windows-1251-to-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!