在php5中為我提供了兩個函數,json_encode和json_decode可以實現json與數組互轉了,但在中文上可以支持不好,下面介紹一個經過處理支持中文數組與json互轉函數,代碼如下:
- <?php
- /**
- * json 生成,分析 支持中文
- */
- class Json_Helper {
- /**
- * 生成json
- */
- public static function encode($str){
- $json = json_encode($str);
- //linux
- return preg_replace("#/u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '/1'))", $json);
- //windows
- //return preg_replace("#/u([0-9a-f]{4})#ie", "iconv('UCS-2LE', 'UTF-8', pack('H4', '/1'))", $json);
- }
- /**
- * 分析json
- */
- public static function decode($str) {
- return json_decode($str);
- }
- }
- ?>
新聞熱點
疑難解答