<?php /** * JSON generation, analysis support 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); }
/** * Analysis JSON */ public static function decode ($STR) { Return Json_decode ($STR); } } ?>
This is another related article from the online search
When encoding data using PHP's own Json_encode, Chinese will become Unicode, resulting in unreadable. For example: After Json_encode the string "Xiamen", the output is "\u53a6\u95e8".
There are two ways to look at the query: 1. Restore "\u53a6\u95e8" to "Xiamen" using the following code:
Also, since Json_encode and Json_decode only support utf-8 encoded characters, the GBK characters have to be converted with JSON, with their own GBK to UTF-8 code:
The code is as follows:
/* The string GBK transcoded to UTF-8, and the numbers are converted to numbers. */ function Ct2 ($s) { if (Is_numeric ($s)) { return intval ($s); } else { Return Iconv ("GBK", "UTF-8", $s); } } /* Batch processing Gbk->utf-8 */ function Icon_to_utf8 ($s) {
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.