PHP json_encode processing GBK and gb2312 Chinese garbled problem solution,
This paper describes the solution of Json_encode processing GBK and gb2312 in PHP, and the method is as follows:
1.json_encode () Chinese returns null for Chinese in gbk/gb2312
$arr = Array ( ' catid ' = ' 4 ', ' catname ' = ' www.jb51.net ', ' meta_title ' = ' home for help ' )); echo Json_encode ($arr);
Operation Result:
[{"CatID": "4", "CatName": "Www.jb51.net", "Meta_title": null}]
Do you have a look? "Meta_title": null he had a value for "Help the House", this we checked the principle is json_encode only support uft-8 encoding, we convert
<?php$data= "JSON Chinese", $newData =iconv ("Gb2312″," "Utf-8//ignore", $data); Echo $newData;//ignore means ignoring errors at the time of conversion, If there is no ignore parameter, all characters following the character are not saved. or ("Gb2312″," Utf-8″, $data); >
2. The Background PHP page (the page encoding is UTF-8 or the character has been converted to UTF-8) uses Json_encode to convert the array arrays in PHP to JSON strings. For example:
<?php$testjson=array (' name ' = = ' Chinese string ', ' value ' = ' test '); Echo Json_encode ($testJSON);? >
View the output as:
{"Name": "U4e2du6587u5b57u7b26u4e32″," "Value": "Test"}
Visible is the use of UTF8 encoded characters, using Json_encode also appeared in Chinese garbled. The solution is to use the character urlencode () before using Json_encode, and then json_encode the output, and then use the function UrlDecode () to turn back. Specific as follows:
<?php$testjson=array (' name ' = = ' Chinese string ', ' value ' = ' test ');//echo Json_encode ($testJSON); foreach ($testJSON As $key + $value) {$testJSON [$key] = UrlEncode ($value);} Echo UrlDecode (Json_encode ($testJSON));? >
View the output as:
{"Name": "Chinese string", "value": "Test"}
Summary: Thejson_encode function can only handle UFT8 strings, if the Chinese estimate is not good for byte processing, because the Chinese GBK and uft length is not the same , this also does not do in-depth introduction.
http://www.bkjia.com/PHPjc/840739.html www.bkjia.com true http://www.bkjia.com/PHPjc/840739.html techarticle PHP json_encode processing GBK and gb2312 Chinese garbled problem resolution method, this article tells the PHP Json_encode processing GBK and gb2312 Chinese garbled problem resolution method, the concrete method is as follows: ...