PHP json_encode processing GBK and gb2312 Chinese garbled problem resolution, _php tutorial

Source: Internet
Author: User
Tags urlencode

PHP json_encode processing GBK and gb2312 Chinese garbled problem solution,


This paper describes the solution of Json_encode processing GBK and gb2312 in PHP, and the method is as follows:

1.json_encode () Chinese returns null for Chinese in gbk/gb2312

$arr = Array (  ' catid ' = '    4 ',    ' catname ' = ' www.jb51.net ',    ' meta_title ' = ' home   for help ' )); echo Json_encode ($arr);

Operation Result:

[{"CatID": "4", "CatName": "Www.jb51.net", "Meta_title": null}]

Do you have a look? "Meta_title": null he had a value for "Help the House", this we checked the principle is json_encode only support uft-8 encoding, we convert

<?php$data= "JSON Chinese", $newData =iconv ("Gb2312″," "Utf-8//ignore", $data); Echo $newData;//ignore means ignoring errors at the time of conversion, If there is no ignore parameter, all characters following the character are not saved. or ("Gb2312″," Utf-8″, $data); >

2. The Background PHP page (the 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": "U4e2du6587u5b57u7b26u4e32″," "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 use the function UrlDecode () to turn back. Specific as follows:

<?php$testjson=array (' name ' = = ' Chinese string ', ' value ' = ' test ');//echo Json_encode ($testJSON); foreach ($testJSON As $key + $value) {$testJSON [$key] = UrlEncode ($value);} Echo UrlDecode (Json_encode ($testJSON));? >

View the output as:

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

Summary: Thejson_encode function can only handle UFT8 strings, if the Chinese estimate is not good for byte processing, because the Chinese GBK and uft length is not the same , this also does not do in-depth introduction.




http://www.bkjia.com/PHPjc/840739.html www.bkjia.com true http://www.bkjia.com/PHPjc/840739.html techarticle PHP json_encode processing GBK and gb2312 Chinese garbled problem resolution method, this article tells the PHP Json_encode processing GBK and gb2312 Chinese garbled problem resolution method, the concrete method is as follows: ...

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