JSON.stringify中的Unicode字符转换为真正的Unicode字符 [英] Unicode characters from JSON.stringify to real unicode characters
本文介绍了JSON.stringify中的Unicode字符转换为真正的Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用JSON.stringify()
函数对用于发送到PHP的AJAX的JS对象进行字符串化.
I use JSON.stringify()
function to stringify JS objects for AJAX sending to PHP.
当JSON.stringify函数将Unicode字符编码为\uxxxx
格式(例如\u000a
)时,就会出现问题.我的问题是如何在PHP中将这些字符转换为常规unicode字符?
The problem arises when JSON.stringify function encodes unicode characters to format \uxxxx
(eg. \u000a
). My question is how to convert those characters to regular unicode characters in PHP?
推荐答案
请参见这将转换为UTF-8:
This converts to UTF-8:
function unescape_utf16($string) {
/* go for possible surrogate pairs first */
$string = preg_replace_callback(
'/\\\\u(D[89ab][0-9a-f]{2})\\\\u(D[c-f][0-9a-f]{2})/i',
function ($matches) {
$d = pack("H*", $matches[1].$matches[2]);
return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
}, $string);
/* now the rest */
$string = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
function ($matches) {
$d = pack("H*", $matches[1]);
return mb_convert_encoding($d, "UTF-8", "UTF-16BE");
}, $string);
return $string;
}
这篇关于JSON.stringify中的Unicode字符转换为真正的Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文