PHP Json_encode processing Chinese characters as null service-side JSON Chinese character coding

Source: Internet
Author: User
Tags php server

PHP Json_encode processing Chinese characters as null service-side JSON Chinese character coding

The reason is that this character is GBK encoded, JSON can only handle UTF-8 encoding, so we have to transcode the data first

When using ExtJS to receive JSON results returned by the PHP server, it is a wonder that any characters (from SQL Server) are empty characters. Later read Json_encode's document only to know that it can only encode UTF-8 characters. The problem is that the returned character is GB2312, how it becomes UTF-8, and it is best to convert the entire array UTF-8. After searching, the following code is done:

Working with array encoding formats

function Td_iconv ($data, $charset _from, $charset _to) {
if (Strtolower ($charset _from) = = "gb2312") {
$charset _from = "GBK";
}
if (Strtolower ($charset _from) = = Strtolower ($charset _to)) {
return $data;
}

if (Is_array ($data)) {
foreach ($data as $k = = $v) {
if (Is_array ($v)) {
$data [$k] = Td_iconv ($v, $charset _from, $charset _to);
}else {
$data [$k] = is_string ($v)? Mb_convert_encoding ($v, $charset _to, $charset _from): $v;
}
}
}else {
if (is_string ($data))
$data = mb_convert_encoding ($data, $charset _to, $charset _from);
}
return $data;
}




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.