Home >php教程 >php手册 >php json与数组互转支持中文

php json与数组互转支持中文

WBOY
WBOYOriginal
2016-06-13 09:50:51853browse

在php5中为我提供了两个函数,json_encode和json_decode可以实现json与数组互转了,但在中文上可以支持不好,下面介绍一个经过处理支持中文数组与json互转函数。

 代码如下 复制代码


/**
 * 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);
 }
}
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn