Phpjson_encode always returns the unicode character "u." problem solved

Source: Internet
Author: User
Phpjson_encode always returns the unicode character u. solve the problem recently encountered a strange problem. The code written two days ago is garbled again today. Check whether the returned unicode characters are not garbled characters in general sense.

For example, Chinese characters: logon failed. after json_encode is passed, the returned result is: "\ u767b \ u5f55 \ u5931 \ u8d25"

I found it a common problem and finally solved it as follows:

Call json_encode ($ arr) normally, and process the json data between echo using the following function:

Public static function JSON ($ str ){
$ Json = json_encode ($ str );
Return preg_replace ("# \\\ u ([0-9a-f] +) # ie", "iconv ('ucos-2', 'utf-8', pack ('h4 ', '\ 1') ", $ json );
}

In this way, there will be no garbled characters. For php of a later version, you can add a parameter directly, but it is invalid for a later version. The complete code is as follows:

 

Call example:

// Echo "logon failed"; $ arr = array (); $ arr ['code'] =-1; $ arr ['message'] = "logon failed "; echo $ arr ['message']; $ arr ['data'] = ""; return util: JSON ($ arr );

Reference: http://stackoverflow.com/questions/6771938/any-way-to-return-php-json-encode-with-encode-utf-8-and-not-unicode

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.