json_encode之后的unicode字符转换为原始字符
json_encode之后的中文会变成'\u4e2d\n\u8d35'这个样子,提供两个函数转换成中文字符。
Python:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def unicode_convert(t):
2 if t:
3 pattern = re.compile('\\\\u[0-9a-f]{4}')
4 t_all = pattern.findall(t)
5
6 if t_all:
7 for o in t_all:
8 n = unichr(eval('0x'+o.replace('\\u', '')))
9 t = t.replace(o, n)
10 return t
2 if t:
3 pattern = re.compile('\\\\u[0-9a-f]{4}')
4 t_all = pattern.findall(t)
5
6 if t_all:
7 for o in t_all:
8 n = unichr(eval('0x'+o.replace('\\u', '')))
9 t = t.replace(o, n)
10 return t
PHP:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function unicode_convert($t) {
2 preg_match_all('/\\\\u[0-9a-f]{4}/', $t, $t_all);
3
4 foreach ($t_all[0] as $ch) {
5 $_ch = intval(str_replace('\u', '0x', $ch), 16);
6 $uni_ch = mb_convert_encoding('&#' . $_ch . ';', 'UTF-8', 'HTML-ENTITIES');
7 $t = str_replace($ch, $uni_ch, $t);
8 }
9
10 return $t;
11 }
2 preg_match_all('/\\\\u[0-9a-f]{4}/', $t, $t_all);
3
4 foreach ($t_all[0] as $ch) {
5 $_ch = intval(str_replace('\u', '0x', $ch), 16);
6 $uni_ch = mb_convert_encoding('&#' . $_ch . ';', 'UTF-8', 'HTML-ENTITIES');
7 $t = str_replace($ch, $uni_ch, $t);
8 }
9
10 return $t;
11 }
----------------------------------------------
我的血管里
流淌着
自由的血液