Home  >  Article  >  Backend Development  >  PHP json and array conversion support Chinese_PHP tutorial

PHP json and array conversion support Chinese_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:48:41908browse

In php5, I was provided with two functions, json_encode and json_decode, which can realize the mutual conversion between json and arrays, but they are not well supported in Chinese. Here is a processed function that supports the mutual conversion between Chinese arrays and json.

The code is as follows
 代码如下 复制代码


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

Copy code

/**
* json generation, analysis supports Chinese
​*/
class Json_Helper {
/**
* Generate 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);
}

/**
* Analyze json
​*/
public static function decode($str) {
           return json_decode($str);
}
}
?>

http://www.bkjia.com/PHPjc/632779.htmlwww.bkjia.com
true
http: //www.bkjia.com/PHPjc/632779.html
TechArticle provides me with two functions in php5, json_encode and json_decode can realize the mutual conversion between json and array, but The support for Chinese is not good. Here is a processed array that supports Chinese...
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