Phpjson_encode Chinese garbled solution

Source: Internet
Author: User
Chinese often encounters problems intentionally or unintentionally in php Development. let's take a look at the json_encode Chinese garbled solution today. Although the solution is not very good, it can solve the problem and the instance generation of the problem.

Chinese often encounters problems intentionally or unintentionally in php Development. let's take a look at the json_encode Chinese garbled solution today. Although the solution is not very good, it can solve the problem. The instance code of the problem is as follows:

  1. $ Arr = array
  2. (
  3. 'Name' => 'chia ',
  4. 'Age' => 20
  5. );
  6. $ Jsonencode = json_encode ($ arr );
  7. Echo $ jsonencode;
  8. ?>

The results are as follows: {"Name": null, "Age": 20}. the Chinese language is empty. why? let's test utf-8. the code is as follows:

  1. $ Array = array
  2. (
  3. 'Title' => iconv ('gb2312', 'utf-8', 'Here is the Chinese title '),
  4. 'Body' => 'abcd ...'
  5. );
  6. Echo json_encode ($ array );
  7. ?>

Result:{"Title": "u8fd9u91ccu662fu4e2du6587u6807u9898", "body": "abcd ..."}

This is not what we want, so we use the php urlencode () function to process the following. before json_encode, we use urlencode () to process all the content in the array, in this way, there is no problem with Chinese.


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.