Home  >  Article  >  Backend Development  >  Solution to Chinese problem in json_encode format in php_PHP tutorial

Solution to Chinese problem in json_encode format in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:53:30758browse

1. Cause analysis:

When storing to the database! MySQL does not store unicode characters:

MySQL only supports basic multilingual flat characters (0×0000-0xFFFF). Please try storing a synonym instead :)

UPDATE: On MySQL 5.5.3 (which is not yet GA), supplementary characters are supported if you use UTF8MB4 encoding.

When json_encode Chinese, each Chinese character will be encoded into "uxxxx"

When saving into the database, "" is blocked and directly becomes "uxxxx"

2. Solve the problem:

Once you know the cause, you can solve the problem. You can choose other storage methods;

Or you can further escape "" to "" to retain ""

Our solution:

 1. Avoid json_encode converting Chinese into unicode encoding.

PHP version 5.4 has added a new option to Json: JSON_UNESCAPED_UNICODE. After adding this option, Chinese will not be automatically encoded.

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

2. First urlencode and json_encode the Chinese field, and then use urldecode to ensure that the Chinese field will not be converted into unicode.

 $test =urldecode(json_encode(array('brief'=>urlencode('Introduction'),'title'=>urlencode(title)));

 3. Further escape "" to "" to avoid '' before Unicode Chinese being removed as a special character by mysql

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371391.htmlTechArticle1. Cause analysis: When storing to the database! MySQL does not store unicode characters: MySQL only supports basic Multilingual flat characters (00000-0xFFFF). Please try storing a synonym opposite...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn