Json encoding method without escaping Chinese characters in php

Source: Internet
Author: User
Although the latest PHP5.4 supports JSON Chinese encoding, it uses the JSON_UNESCAPED_UNICODE parameter, for example: json_encode (quot; Chinese quot;, JSON _... although the latest PHP 5.4 supports JSON Chinese encoding, it uses the JSON_UNESCAPED_UNICODE parameter, for example:

Json_encode ("Chinese", JSON_UNESCAPED_UNICODE) for earlier PHP versions, the json encoding of Chinese characters is not escaped.

The interaction between PHP and JavaScript is actually very convenient, and PHP native also provides support for JSON format. Mainly includes two functions: JSON encoding and decoding:

Json_endoce: http://cn.php.net/json_encode

Json_dedoce: http://cn.php.net/json_decodejson_encode

Encode the variable in JSON format and return the value in JSON format, for example:

 1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr);?>

After the above code is executed, the output is {"a": 1, "B": 2, "c": 3, "d": 4, "e": 5}

Assume that the data source to be encoded (usually an array) contains Chinese characters. after json_encode processing, unicode encoding is output. the code is as follows:

 'Mango station '); echo json_encode ($ arr);?>

The above code is output after execution. the code is as follows:

{"A": "u8292u679Cu5C0Fu7AD9 "}

Unicode processing has been performed at the underlying layer of PHP. if it is not intuitive enough, you can use urlencode and urldecode to bypass the unicode transcoding process. The code is as follows:

$ Arr = array ('a' => urlencode ('Mango Xiaozhan '));

Echo urldecode (json_encode ($ arr); output after the above code is executed: {"a": "mango site "}

Here is a more perfect method:

 


Permanent link:

Reprint at will! Include the article address.

Related Article

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.