How to solve the problem of Chinese display in PHPJSON format, _ PHP Tutorial

Source: Internet
Author: User
Tags php json
Solution to the problem of Chinese display in PHPJSON format ,. Solution to the problem of Chinese display in PHPJSON format: return the question of Chinese display in json data in the previous article, return the Chinese display in json format to u5723u8bdeu8282u5343u4e07u597d Chinese display in php json format. solution,

Returns a question about Chinese display of json data.

In the previous article, the returned Chinese characters in json format are displayed as \ u5723 \ u8bde \ u8282 \ u5343 \ u4e07 \ u597d \ u793c \ u5927 \ u5949 \ u9001

Solution 1:
The code is as follows:
<? Php
Function Notice (){
Include './include/conn. php'; // Database link file
$ SQL _notice = mysql_query ('select * FROM gg_notice where enable = "1" limit 0, 10 ');
$ Notice = mysql_fetch_array ($ SQL _notice, MYSQL_ASSOC );
$ Str = json_encode ($ notice );
// Linux
Return preg_replace ("#\\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2be', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str );
// Windows
// Return preg_replace ("# \\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2le', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str );

}
?>

Other methods searched from the internet
The code is as follows:
<? Php
/**
* Json generation and analysis support Chinese characters
*/
Class Json_Helper {
/**
* Generate json
*/
Public static function encode ($ str ){
$ Json = json_encode ($ str );
// Linux
Return preg_replace ("#\\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2be', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ json );
// Windows
// Return preg_replace ("# \\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2le', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ json );
}

/**
* Json analysis
*/
Public static function decode ($ str ){
Return json_decode ($ str );
}
}
?>

This is another related article from the Internet.

When you use the json_encode that comes with php to encode the data, the Chinese character will become unicode, leading to unreadable. For example, after json_encode is performed on the string "Xiamen", the output is "\ u53a6 \ u95e8 ".

The following two methods are available:
1. restore "\ u53a6 \ u95e8" to "Xiamen" and use the following code:
The code is as follows:
$ Str = preg_replace ("# \\\ u ([0-9a-f] +) # ie", "iconv ('ucs-2', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str );

2. urlencode, json_encode, and urldecode are used to display Chinese characters.
The code is as follows:
$ Code = urldecode (json_encode (urlencode ("Xiamen ")));

PHP5.4 has added the option JSON_UNESCAPED_UNICODE to Json. After this option is added, Chinese characters are not automatically encoded.
The code is as follows:
Echo json_encode ("Xiamen", JSON_UNESCAPED_UNICODE );

In addition, because json_encode and json_decode only support UTF-8 characters, GBK characters must be converted to json, with their own write GBK to UTF-8 code:
The code is as follows:
/*
The string GBK is transcoded to a UTF-8, and the number is converted to a number.
*/
Function ct2 ($ s ){
If (is_numeric ($ s )){
Return intval ($ s );
} Else {
Return iconv ("GBK", "UTF-8", $ s );
}
}
/*
Batch processing gbk-> UTF-8
*/
Function icon_to_utf8 ($ s ){

If (is_array ($ s )){
Foreach ($ s as $ key => $ val ){
$ S [$ key] = icon_to_utf8 ($ val );
}
} Else {
$ S = ct2 ($ s );
}
Return $ s;

}

Echo json_encode (icon_to_utf8 ("Xiamen "));

Solve the problem of Chinese display in JSON format of http://www.bkjia.com/PHPjc/981352.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/981352.htmlTechArticlePHP, return the problem of Chinese display in json data in the previous article, the returned json format is displayed in Chinese as \ u5723 \ u8bde \ u8282 \ u5343 \ u4e07 \ u597d...

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.