PHP Json_encode () function and Chinese garbled problem, _php tutorial

Source: Internet
Author: User

PHP Json_encode () function in detail and Chinese garbled problem,


Using the Json_encode () built-in function in PHP (PHP > 5.2) can be used in PHP to communicate well with other languages and use it.

The function is to convert a numeric value into a JSON data storage format.

<?php$arr = array  (   ' Name ' = ' Shia ',   ' age ' =  ); $jsonencode = Json_encode ($arr); echo $ Jsonencode;? >

The results of the program run as follows:

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

Json_encode function in Chinese is encoded as NULL, Google a bit, very simple, in order to closely integrate with the front, Json only support utf-encoding, I think is 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 result of this program is:

{"title": "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898", "Body": "ABCD ..."}

All the Chinese in the array have disappeared after the Json_encode, or appear \u2353.

The

Workaround is to use the UrlEncode () function to process the following, Json_encode all the contents of all arrays with UrlEncode (), and then convert the Json_encode () to a JSON string, and then use the UrlDecode ( ) To turn the encoded Chinese back.

<?php/************************************************************** * * Handle all elements in an array with a specific function * @param string  & $array string to be processed * @param string $function function to execute * @return Boolean $apply _to_keys_also is also applied to 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_key  S_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 (Chinese language compatible) * @param array $array the arrays to be converted * @ return string The converted JSON String * @access public * *************************************************************/function JSON ($array) { Arrayrecursive ($array, ' UrlEncode ', true); $json = Json_encode ($array); Return UrlDecode ($json);} $array = Array (' Name ' = ' Shia ', ' age ' =); echo JSON ($array); >

This success, the results of the operation are as follows:

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

The following is to introduce PHP json_encode Chinese garbled solution

I believe a lot of people in the use of Ajax and background PHP pages to interact with the problem of Chinese garbled. As a lightweight data interchange format, JSON is pro-gaze, but with PHP as a background interaction, it is easy to get the problem of Chinese garbled. JSON and JS, for the client's characters are processed in the form of UTF8, that is, the use of JSON as the submitted and received data format when the characters are UTF8 encoding processing, when our page encoding and database encoding is not the use of UTF8, The problem of Chinese garbled characters is very easy to appear. The solution is to use JS or PHP when processing JSON data in the form of UTF8 .

PHP5.2 or above version of Json_encode as built-in functions to use, to the site-based authors to bring great convenience, but we must note that Json_encode only support UTF8 encoded characters, otherwise, the Chinese garbled or empty values appear.

The solution is divided into the following two steps.

Step1

Ensure that characters are encoded in UTF8 when using JSON processing. Specifically, we can change the database encoding and page encoding to UTF8. Of course, if you like to use GBK encoding, you can convert the characters to UTF8 form before JSON processing. In PHP, there are the following methods:

<?php   $data = "json Chinese";   $newData =iconv ("GB2312", "Utf-8//ignore", $data);   echo $newData;   Ignore means ignoring errors at the time of conversion, and if there is no ignore argument, all characters following the character will not be saved.   

Step2

The Background PHP page (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": "\u4e2d\u6587\u5b57\u7b26\u4e32″," "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 turn back with the function UrlDecode (). Specific as follows:

<?php  $testJSON =array (' name ' = = ' Chinese string ', ' value ' = ' test ');  echo Json_encode ($testJSON);  foreach ($testJSON as $key = + $value) {   $testJSON [$key] = UrlEncode ($value);  }  

View the output as:

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

To this, the Chinese characters are successfully exported. Feel free to use the Json_encode bar. In this way, the JSON string output in the PHP background in the foreground JavaScript Ajax received after the eval will not appear in Chinese garbled, because JS in the processing of JSON format data is also in the form of UTF8, similar to PHP, Therefore, the JSON string that receives the PHP page does not appear to be problematic.

http://www.bkjia.com/PHPjc/1068832.html www.bkjia.com true http://www.bkjia.com/PHPjc/1068832.html techarticle php Json_encode () function in detail and Chinese garbled problem, in PHP using the Json_encode () built-in function (PHP 5.2) can be used in PHP data can be very good with other languages and make ...

  • 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.