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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10451058/
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-10-26 09:56:11  来源:igfitidea点击:

converting unicode \u sequence (like \u041a) to normal utf-8 text in php

phpjavascriptutf-8json

提问by Ekonoval

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

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

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

alert("\u041a\u043b\u0443\u0431 测试");

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 中做同样的事情,有没有内置方法?

enter image description here

在此处输入图片说明

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

采纳答案by globin

You probably want HTML entities to print the characters:

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

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

回答by Starx

While converting the data, use JSON_UNESCAPED_UNICODEas the options

转换数据时,JSON_UNESCAPED_UNICODE用作选项

echo json_encode($text, JSON_UNESCAPED_UNICODE);