将unicode \ u序列(如\ u041a)转换为php中的普通utf-8文本

时间:2021-03-28 00:26:37

I have a string like this "\u041a\u043b\u0443\u0431 Test";

我有一个像这样的字符串“\ u041a \ u043b \ u0443 \ u0431测试”;

It was decoded by json_encode(), the original string was "Клуб Test" in russian. when I put it to js like

它由json_encode()解码,原始字符串是俄语中的“КлубTest”。当我把它放到js之类的时候

alert("\u041a\u043b\u0443\u0431 Test");

I get correct displaying, like on the screen. So js in some way poperly decodes it to normal view. The question is how can I do the same thing in php, is there any built in method?

我得到正确的显示,就像在屏幕上一样。因此js以某种方式将其正确地解码为普通视图。问题是如何在php中执行相同的操作,是否有内置方法?

将unicode \ u序列(如\ u041a)转换为php中的普通utf-8文本


THE ANSWER IS:
$json_in = '{"testKey":"\u041a\u043b\u0443\u0431 Test"}';
$json_out = json_decode($json_in, true);
or
Convert  "\u041a\u043b\u0443\u0431" to  "Клуб" 
and perform html_entity_decode($str, null, 'UTF-8');

2 个解决方案

#1


1  

You probably want HTML entities to print the characters:

您可能希望HTML实体打印字符:

  • Ӓ for decimal code
  • Ӓ代表十进制代码

  • Ī for hex code
  • Ī对于十六进制代码

#2


5  

While converting the data, use JSON_UNESCAPED_UNICODE as the options

转换数据时,请使用JSON_UNESCAPED_UNICODE作为选项

echo json_encode($text, JSON_UNESCAPED_UNICODE);

#1


1  

You probably want HTML entities to print the characters:

您可能希望HTML实体打印字符:

  • Ӓ for decimal code
  • Ӓ代表十进制代码

  • Ī for hex code
  • Ī对于十六进制代码

#2


5  

While converting the data, use JSON_UNESCAPED_UNICODE as the options

转换数据时,请使用JSON_UNESCAPED_UNICODE作为选项

echo json_encode($text, JSON_UNESCAPED_UNICODE);