PHPjson_encode () function explanation and Chinese Garbled text

Source: Internet
Author: User
This article mainly introduces PHPjson_encode () function details and relevant information about Chinese Garbled text. For more information, see

This article mainly introduces the explanation of the PHP json_encode () function and relevant information about Chinese Garbled text. For more information, see

The json_encode () built-in function (php> 5.2) in php can be used to transmit and use data in php with other languages.

This function converts a value to a json data storage format.

<? Php $ arr = array ('name' => 'ya', 'age' =>); $ jsonencode = json_encode ($ arr); echo $ jsonencode;?>

The program running result is as follows:

{"Name": null, "Age ":}

In the json_encode function, Chinese characters are encoded as null. Google: Very simple. To work closely with the front-end, Json only supports utf-encoding, I think the front-end Javascript is also the reason for utf.

<? Php $ array = array ('title' => iconv ('gb', 'utf-', 'Here is the Chinese title'), 'body' => 'abcd... '); echo json_encode ($ array);?>

The running result of this program is:

{"Title": "\ u8fd9 \ u91cc \ u662f \ u4e2d \ u6587 \ u6807 \ u9898", "body": "abcd ..."}

All Chinese characters in the array are lost after json_encode or \ u2353 is displayed.

The solution is to use the urlencode () function to process the following. Before json_encode, urlencode () is used to process all the content in the array, and json_encode () is used to convert the content into a json string, finally, use urldecode () to convert the Encoded chinese characters back.

<? Php /************************************** * ************************ use a specific function to process all elements in the array * @ param string & $ array the string to be processed * @ param string $ function the function to be executed * @ return boolean $ apply_to_keys_also is also applied to the key * @ access public ******* **************************************** * *************/function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false) {static $ recursive_counter =; if (++ $ Recursive_counter>) {die ('possible deep recursion attack');} foreach ($ array as $ key => $ value) {if (is_array ($ value )) {arrayRecursive ($ array [$ key], $ function, $ apply_to_keys_also);} else {$ array [$ key] = $ function ($ value );} if ($ apply_to_keys_also & is_string ($ key) {$ new_key = $ function ($ key); if ($ new_key! = $ Key) {$ array [$ new_key] = $ array [$ key]; unset ($ array [$ key]) ;}}$ recursive_counter --;} /*************************************** * ************************ convert an array to a JSON string (compatible with Chinese characters) * @ param array $ array the array to be converted * @ return string the converted json string * @ access public ***************** **************************************** * ***/function JSON ($ array) {arrayRecursive ($ array, 'urlencode', true); $ json = json_enco De ($ array); return urldecode ($ json);} $ array = array ('name' => 'xia', 'age' => ); echo JSON ($ array);?>

The operation is successful. The result is as follows:

{"Name": "Sia", "Age": "20 "}

The following describes how to solve PHP json_encode garbled characters.

I believe many people have encountered Chinese garbled characters when using Ajax to interact with backend php pages. JSON, as a lightweight data exchange format, is widely used. However, PHP is used for background interaction, which is prone to Chinese garbled characters. JSON and js are the same. The client-side characters are processed in the form of UTF8. That is to say, when JSON is used as the format of submitted and received data, UTF-8 encoding is used, when we do not use UTF8 for page encoding and database encoding, Chinese garbled characters are very likely to occur.The solution is UTF8 when JSON data is processed using js or PHP..

PHP5.2 or later versions use json_encode as built-in functions, which makes it very convenient for website creators. However, we must note that json_encode only supports UTF-8 characters. Otherwise, chinese characters are garbled or null.

The solution consists of the following two steps.

Step 1

Make sure that the characters are UTF-8 encoded during JSON processing. For details, we can change the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the character into UTF8 format before JSON processing. The following methods are available in PHP:

<? Php $ data = "JSON Chinese"; $ newData = iconv ("GB2312", "UTF-8 // IGNORE", $ data); echo $ newData; // ignore indicates that the conversion error is ignored. Without the ignore parameter, all characters after this character will not be saved. // Or ("GB2312", "UTF-8", $ data);?>

Step 2

The background PHP page (the page is encoded as a UTF-8 or has converted the character into a UTF-8) uses json_encode to convert the array in PHP into a JSON string. For example:

<? Php $ testJSON = array ('name' => 'Chinese string', 'value' => 'test'); echo json_encode ($ testJSON);?>

The output result is as follows:

{"Name": "\ u4e2d \ u6587 \ u5b57 \ u7b26 \ u4e32", "value": "test "}

It can be seen that UTF-8 characters are used, and Chinese garbled characters are also used in json_encode. The solution is to use the urlencode () function to process the characters before using json_encode, and then use json_encode to return the urldecode () function when outputting the results. The details are 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);?>

The output result is as follows:

{"Name": "Chinese string", "value": "test "}

At this point, Chinese characters are successfully output. Use json_encode at will. In this way, the JSON string output in the PHP background will not contain Chinese garbled characters After Ajax is received in the front-end javascript, because js is also processing JSON format data in the form of UTF8, similar to PHP, The JSON string that receives the PHP page will not be faulty.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.