PHP JSON format for Chinese display problem solving method _php

Source: Internet
Author: User
Keywords Php json format Chinese display problem
Tags php json
Returns the problem of displaying JSON data in Chinese

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

Workaround One:

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 ways to search from the Internet

The code is as follows:


<?php
/**
* JSON generation, analysis support Chinese
*/
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);
}

/**
* Analysis JSON
*/
public static function decode ($STR) {
Return Json_decode ($STR);
}
}
?>

This is another related article from the online search

When encoding data using PHP's own Json_encode, Chinese will become Unicode, resulting in unreadable. For example: After Json_encode the string "Xiamen", the output is "\u53a6\u95e8".

There are two ways to look at the query:
1. Restore "\u53a6\u95e8" to "Xiamen" using 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. After urlencode,json_encode the Chinese text paragraph, then use the UrlDecode, you can also display the English language.

The code is as follows:


$code = UrlDecode (Json_encode (UrlEncode ("Xiamen"));

PHP5.4 version, a new option has been added to JSON: Json_unescaped_unicode. With this option, the Chinese is not automatically encoded.

The code is as follows:


Echo Json_encode ("Xiamen", Json_unescaped_unicode);


Also, since Json_encode and Json_decode only support utf-8 encoded characters, the GBK characters have to be converted with JSON, with their own GBK to UTF-8 code:

The code is as follows:


/*
The string GBK transcoded to UTF-8, and the numbers are converted to numbers.
*/
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"));

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