首頁 > 後端開發 > php教程 > php如何将json中的unicode编码转为汉字?

php如何将json中的unicode编码转为汉字?

WBOY
發布: 2016-06-23 14:15:00
原創
1034 人瀏覽過

有两个json数据
$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"\u519c\u592b\u5c71\u6cc9","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]
$c=[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"\u7ef4\u8fbe\u7eb8\u5dfe","cls":{"id":"125","name":"\u65e5\u7528\u54c1"},"std":{"name":"\u5f20\u6570","val":"10"}}]

如何将其中unicode字符转为汉字,使得

$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"农夫山泉","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]
$c=[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"维达纸巾","cls":{"id":"125","name":"日用品"},"std":{"name":"张数","val":"10"}}]

哪位知道的话帮忙解答一下,先谢谢了~~


回复讨论(解决方案)

linux
$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);

windows
$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8', pack('H4', '……

引用 3 楼 的回复:

linux
$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);

windows
$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8'……

不行啊,是这样写吗
$c=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8', pack('H4','\\1'))", $c);
echo $c;
$c的输出是
[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"?倦?搿撅?","cls":{"id":"125","name":"?モ〉??"},"std":{"name":"???","val":"10"}}]
有乱码啊,我是windows平台,麻烦再帮忙看些怎么解决?

怎么说不行呢

我试了可以,

$str = '$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"\u519c\u592b\u5c71\u6cc9","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]
$c=[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"\u7ef4\u8fbe\u7eb8\u5dfe","cls":{"id":"125","name":"\u65e5\u7528\u54c1"},"std":{"name":"\u5f20\u6570","val":"10"}}]';

$str=preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
echo $str;



乱码的话记得换下浏览器编码。

编码以后 记得用utf-8

汉字和编码 没区别的 我知道你的意思 嘿嘿~只要utf-8 传输 就没问题

怎么说不行呢

我试了可以,

$str = '$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"\u519c\u592b\u5c71\u6cc9","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]
$c=[{"_id":{"$id":"4fdaa7f3741d7……
怎么我的显示不了,结果是这样:
$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"??か灞辨?","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}] $c=[{"_id":{"$id":"4fdaa7f3741d725816000000"},"bc":"012345678","name":"缁磋揪绾稿肪","cls":{"id":"125","name":"?ョ???"},"std":{"name":"寮??","val":"10"}}]
用ie浏览器也不行,怎么回事?求解~~

怎么说不行呢

我试了可以,

$str = '$d=[{"_id":{"$id":"4fda7d42741d727c14000000"},"name":"\u519c\u592b\u5c71\u6cc9","bc":"123456","pic":"d: pic\water.jpg","aid":"232fd4df3"}]
$c=[{"_id":{"$id":"4fdaa7f3741d7……
哦~~是的,是的。加了header("Content-type: text/html; charset=utf-8");就好了,
多谢你帮忙解答。

赞一个 轻松搞定

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板