PHP 将 Windows-1251 转换为 UTF 8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15706077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:47:12  来源:igfitidea点击:

PHP Convert Windows-1251 to UTF 8

phputf-8character-encodingwindows-1251

提问by user2058653

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

我有一个小的 html 代码,我需要将它转换为 UTF-8。
我用这个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 Р?Р?Р?азаС?С? Р?Р?

所有文本都正确转换,但如果文本例如在 tag 中<i>...</i>,那么它不会转换文本,我看到这样的东西Р?Р?Р?азаС?С? Р?Р?

回答by Jeff Cashion PhD

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

如果您可以访问 Multibye 包,您可以尝试一下。请参阅此处的 PHP 页面:http: //www.php.net/manual/en/function.mb-convert-encoding.php

$html_utf8 = mb_convert_encoding($html, "utf-8", "windows-1251");

回答by Артем Николаев

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-8and this text will shown correctly.

您知道,消息就像Р?Р?Р?азаС?С? Р?Р?您看到的页面编码是否为windows-1251,但文本以utf-8.
我在我的一个项目中看到了这个问题,因此只需更改页面输入的更改编码utf-8,此文本将正确显示。

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

让我举一些例子:
如果 page in utf-8,但是 text inwindows-1251你会看到这样的东西:
???? ?? ?????? ??? ????? ??? ??????? ?? ????? ???? ??? ?????

if page in windows-1251, but text in utf-8you see this:
"Р?Р?Р±РёР?С?Р?С?Рμ С?РμР?РμС?Р?Р?С?";"Apple iPhone 4

如果 page in windows-1251,但是utf-8你看到的文字是:
"Р?Р?Р±РёР?С?Р?С?Рμ С?РμР?РμС?Р?Р?С?";"Apple iPhone 4

回答by T.Todua

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

ps 不要忘记,.php 文件编码应该是UTF8。另外,在 HTML 的头部,插入UTF8 的标准声明

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

回答by Sergey

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.

大多数解决方案缺乏向单字节编码的转换。在我的情况下,我使用 mb_convert_encoding($string,'windows-1251') 从 UTF-8 转换。

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;

}

回答by Xose da Ria

try this, works for me!

试试这个,对我有用!

$result = str_replace ('?', 'a??' , $result);