PHP to solve the JSON Chinese garbled function _php Tutorial

Source: Internet
Author: User
PHP tutorial comes with Json_encode to process JSON data, then their support for Chinese is not good, let's look at an example

echo Json_encode (Array (123213, ' China '));


{"PlatformID": "123213", "userid": "1023", "username": "U00B7U00F0U00B5U00B2U00C9U00B1U00B7U00F0U00CCU00FC"}

We will find that the English can be correctly parsed, but Chinese will come out U00B7U00F0U00B5U00B2U00C9U00B1U00B7U00F0U00CCU00FC Oh, this may be Unicode encoding, but I did not test it just to guess. Let's look at a function to solve the JSON Chinese garbled

Private Function To_utf8 ($in)
{
if (Is_array ($in)) {
foreach ($in as $key = $value)
{
$out [$this->to_utf8 ($key)] = $this->to_utf8 ($value);
}
}
ElseIf (is_string ($in))
{
if (Mb_detect_encoding () ($in)! = "Utf-8")
Return Utf8_encode ($in);
Else
return $in;
}
Else
{
return $in;
}
return $out;
}

1. Put $usr->username direct output, page header set Charset=utf-8. garbled
2.echo json_encode ($usr) Output username=null
3. The page header is set to CHARSET=GBK, the output is correct, can be determined that the original code is GBK
Finally, the conclusion is obtained by Ie,chrome,firefox test:


1. Ensure that the page character set is consistent with the database tutorial, the output must be normal.
2. Ensure that data encoding is utf-8,json_decode normal when doing json_encode.
3. If you want to do json_encode for non-utf-8 characters, first convert to Utf-8.
4. For non-utf-8 characters do json_decode, do not forget to convert to the original code, otherwise it will output garbled!!

http://www.bkjia.com/PHPjc/632302.html www.bkjia.com true http://www.bkjia.com/PHPjc/632302.html techarticle PHP Tutorial comes with Json_encode to process JSON data, then their support for Chinese is not good, let's look at an example of Echo Json_encode (Array (123213, ' China ')); {Platformid:123213,u ...

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