How to solve the problem in json_encode format in php

Source: Internet
Author: User
Today, we will store json_encode format information in php in MySQL. there is no problem in English, but when it comes to Chinese, it will become a bunch of information similar to uxxxx. 1. cause analysis:

Today, we will store json_encode format information in php in MySQL. there is no problem in English, but when it comes to Chinese, it will become a bunch of information similar to uxxxx.

1. cause analysis:MySQL does not store unicode characters when it is stored in the database. MySQL only supports basic multilingual flat characters (0x0000-0 xFFFF). please try to store the opposite synonym :).

Update: MySQL 5.5.3 (not yet GA). supplementary characters are supported. if you use UTF8MB4 encoding and json_encode Chinese, each Chinese character is encoded as "uxxxx ", when the database is saved, "" is blocked and directly changed to "uxxxx ".

2. solve the problem:If you know the reason, you can solve the problem. you can choose another storage method, or escape "\" to keep "".

Our solution:

1. to prevent json_encode from converting Chinese to unicode encoding. PHP5.4, you have added an option for Json: JSON_UNESCAPED_UNICODE. After this option is added, Chinese characters are not automatically encoded.

$ Test = json_encode ("Shenzhen", JSON_UNESCAPED_UNICODE );

2. urlencode, json_encode, and urldecode are used to ensure that Chinese characters are not converted to unicode.

  1. $ Test = urldecode (json_encode (array ('brief '=> urlencode ('Introduction'), 'title' => urlencode (title )));
  2.  

3. escape "" as "\" to avoid removing the unicode Chinese prefix ''from mysql as a special character

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.