PHP self-contained function json_encode

Source: Internet
Author: User

PHP scenario, you need to convert the array to a JSON string, you need to use PHP's own json_encode function;

But when the array contains Chinese strings, the result of the transfer is the following:

1<?PHP2 3     $TMPARR=Array(4' Name ' = ' trousers ',5' Color ' = ' blue ',6' Size ' = ' XL ',7' title ' and ' Aged mens trousers '8     );9 Ten     $tmpJson= Json_encode ($TMPARR); One  A     Echo $tmpJson;

Output: {"name": "\u957f\u88e4", "Color": "Blue", "Size": "XL", "title": "\u4e2d\u5e74\u7537\u88c5 \u957f\u88e4"}

Need to Chinese not be converted, only need to give Json_encode function to pass in a parameter Json_unescaped_unicode can, as follows:

1<?PHP2 3     $TMPARR=Array(4' Name ' = ' trousers ',5' Color ' = ' blue ',6' Size ' = ' XL ',7' title ' and ' Aged mens trousers '8     );9 Ten     $tmpJson= Json_encode ($TMPARR,json_unescaped_unicode); One  A     Echo $tmpJson;

Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}

However, the above parameter json_unescaped_unicode is not supported in PHP version <5.4.0 and can be resolved in the following ways

1 $TMPARR=Array(2' Name ' = ' trousers ',3' Color ' = ' blue ',4' Size ' = ' XL ',5' title ' and ' Aged mens trousers '6     );7 8     $tmpJson= Json_encode ($TMPARR);9     $tmpJson=Preg_replace_callback("#\\\u ([0-9a-f]{4}) #i",function($matchs){Ten         return Iconv(' Ucs-2be ', ' UTF-8 ',Pack(' H4 ',$matchs[1])); One},$tmpJson); A  -     Echo $tmpJson;

Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}

Finally, you can encapsulate a function (mainly the test environment is different from the production environment, so encapsulate a function that can be used only in one way depending on your PHP environment):

1<?PHP2     $TMPARR=Array(3' Name ' = ' trousers ',4' Color ' = ' blue ',5' Size ' = ' XL ',6' title ' and ' Aged mens trousers '7     );8 9     functionJson_encode_array ($array){Ten         if(Version_compare(php_version, ' 5.4.0 ', ' < ')){ One             $str= Json_encode ($array); A             $str=Preg_replace_callback("#\\\u ([0-9a-f]{4}) #i",function($matchs){ -                 return Iconv(' Ucs-2be ', ' UTF-8 ',Pack(' H4 ',$matchs[1])); -},$str); the             return $str; -}Else{ -             returnJson_encode ($array,json_unescaped_unicode); -         } +     } -  +     EchoJson_encode_array ($TMPARR);

Output: {"name": "Trousers", "color": "Blue", "Size": "XL", "title": "Middle-aged men's trousers"}

PHP self-contained function json_encode

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.