Php does not escape the json encoding method of Chinese characters _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php does not escape the json encoding method of Chinese characters. Although the latest PHP5.4 supports JSON Chinese encoding, it uses the JSON_UNESCAPED_UNICODE parameter, for example, json_encode (Chinese, JSON_UNESCAPED_UNICODE) for earlier PHP versions, 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 processing does not escape the json encoding of Chinese characters. I have written this article about PHP output of Chinese JSON strings, here is a more perfect method:

/**
* Json encoding method without escaping Chinese characters
* @ Param array $ array to be encoded
* @ Return string
*/
Function encode ($ arr ){

$ Str = json_encode ($ arr );
$ Search = "# \\\ u ([0-9a-f] +) # ie ";
$ Replace = "iconv ('ucos-2', 'utf-8', pack ('h4 ',' \ 1 '))";

Return preg_replace ($ search, $ replace, $ str );

}

Source: Mango Xiaozhan

PHP 5.4 already supports JSON Chinese encoding, that is, using the JSON_UNESCAPED_UNICODE parameter, for example: json_encode (Chinese, JSON_UNESCAPED_UNICODE) for earlier PHP versions ,...

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.