json字符转换成中文方法

 1 public function json_encode($array)
 2     {
 3         $returnStr = '';
 4         $str = @json_encode($array);
 5         if (preg_match_all("/\\\u[a-z0-9]{4}|[\w\W]+/U", $str, $matchs)) {
 6             $strArr = array();
 7             foreach ($matchs[0] as $_key => $_val) {
 8                 if (substr($_val, 0, 2) == '\u') {
 9                     $charInt = unpack('n', pack("H4", substr($_val,-4)));
10                     $strArr[$_key] = mb_decode_numericentity('&#'.$charInt[1].';', array(0x80, 0xFFFF, 0, 0xFFFF), 'UTF-8');
11                 } else {
12                     $strArr[$_key] = $_val;
13                 }
14             }
15             $returnStr = implode('', $strArr);
16         } else {
17             $returnStr = $str;
18         }
19 
20         return $returnStr;
21     } 

例:{"data":{"ucode":"test2","sname":"[\u4e8c\u670d]\u5343\u53e4\u98ce\u6d41"}}     => {"data":{"ucode":"test2","sname":"[二服]千古风流"}}

 

posted @ 2016-04-08 12:02  多多喜  阅读(1508)  评论(0编辑  收藏  举报