我有这样的字符串:
Óâàæàåìûéêëèåíò!
并希望将其解码为西里尔符号。我已经尝试按mb_convert_encoding进行解码,但没有得到正确的结果。
$string = 'Óâàæàåìûé êëèåíò!';
$stringEncode = mb_detect_encoding($string);
$result = mb_convert_encoding($string, "CP1251", $stringEncode);
echo $result //????????? ??????!
//Case with auto detect encoding return the same result
$result = mb_convert_encoding($string, "CP1251");
echo $result //????????? ??????!
我尝试使用不同的字符编码但总是得到错误的结果。
正确的结果必须是:
Уважаемыйклиент!
注意!我尝试使用在线services对当前字符串进行编码并获得正确的结果。字符串没有被破坏。似乎PHP无法定义编码并将当前字符串转换为西里尔字母。
Thnx任何帮助!
UPD:
bin2hex输出: c393c3a2c3a0c3a6c3a0c3a5c3acc3bbc3a920c3aac3abc3a8c3a5c3adc3b22120c387c3a0c3a2c3b2c3b0c3a020c3adc3a5c3aec3a1c3b5c3aec3a4c3a8c3acc3ae20c3a2c3adc3a5c3b1c3b2c3a820c3acc3a8c3adc3a8c3acc3a0c3abc3bcc3adc3bbc3a920c3afc3abc3a0c3b2c3a5c3a620c3afc3ae20c3a7c3a0c3a9c3acc3b320c3a220c3b0c3a0c3a7c3acc3a5c3b0c3a52036343120c3b0c3b3c3a1c3abc3a5c3a92e20c384c3abc3bf20c3aec3afc3abc3a0c3b2c3bb20c3a2c3aec3b1c3afc3aec3abc3bcc3a7c3b3c3a9c3b2c3a5c3b1c3bc20c3abc3a8c3b7c3adc3bbc3ac20c3aac3a0c3a1c3a8c3adc3a5c3b2c3aec3ac20707572652e636f6d2e7275
“那根弦从哪里来?” - 最初我以json格式得到Api的响应,然后我使用utf8_encode(如果我不使用这个函数json_decode返回null),最后json_decode返回一个数据数组:
[
'status' => '1',
'last_date' => '15.05.2018 10:00:17',
'last_timestamp' => '1526353217',
'send_date' => '15.05.2018 10:00:05',
'send_timestamp' => '1526353205',
'phone' => '79270212817',
'cost' => '6.24',
'sender_id' => 'PURE',
'status_name' => 'Äîñòàâëåíî',
'message' => 'Óâàæàåìûé êëèåíò!'
];
答案 0 :(得分:1)
根据deceze建议,我得到了我的原始Api响应的编码(windows-1251)。比我重写我的'准备json_decode'代码并获得正确的结果。
//Replace this:
$contents = utf8_encode($response);
//To this:
$contents = mb_convert_encoding($response, 'utf-8', 'windows-1251');
$result = json_decode($contents);
注意! utf8_encode将 ISO-8859-1 转换为 UTF-8 ,如果我们使用其他编码传递数据(在我的情况下, windows-1251 < / strong>)进入这个功能,我们会收到意想不到的结果。 @sulquin和@dezeze的大thnx帮我找出了这个问题。
PS:总是检查源数据的编码,不要重复我的错误:)
答案 1 :(得分:0)
我认为您可能认为PHP无法处理此字符串中的某些字符。我做了以下故障排除,但未能找到问题。似乎mb_check_encoding
返回转换应该是可能的,但由于某种原因它不起作用......
您可能需要进行手动转换:PHP Convert Windows-1251 to UTF 8
<?php
$utf8_string = 'Óâàæàåìûé êëèåíò!';
$cp1251_string = 'Уважаемый клиент!';
$utf8_detect = mb_detect_encoding($utf8_string, 'UTF-8');
$cp1251_detect = mb_detect_encoding($cp1251_string, 'CP1251');
$utf8_to_cp1251_check = mb_check_encoding($utf8_string, $cp1251_detect);
$cp1251_to_utf8_check = mb_check_encoding($cp1251_string, $utf8_detect);
$utf8_to_cp1251 = mb_convert_encoding($utf8_string, $cp1251_detect);
$cp1251_to_utf8 = mb_convert_encoding($cp1251_string, $utf8_detect);
$utf8_to_cp1251_icon = iconv( "UTF-8","CP1251//TRANSLIT", $utf8_string);
var_dump($utf8_string);
var_dump($cp1251_string);
echo PHP_EOL;
var_dump($utf8_detect);
var_dump($cp1251_detect);
echo PHP_EOL;
var_dump($utf8_to_cp1251_check);
var_dump($cp1251_to_utf8_check);
echo PHP_EOL;
var_dump($utf8_to_cp1251);
var_dump($cp1251_to_utf8);
echo PHP_EOL;
var_dump($utf8_to_cp1251_icon);
输出
string(32) "Óâàæàåìûé êëèåíò!"
string(32) "Уважаемый клиент!"
string(5) "UTF-8"
string(12) "Windows-1251"
bool(true)
bool(true)
string(17) "????????? ??????!"
string(32) "Уважаемый клиент!"
string(18) "???ae????? ??????!"