PHP's JSON chinese processing solution _php Tips

Source: Internet
Author: User
Tags getting started with php php database php json urlencode

This article describes the JSON Chinese processing solution for PHP. Share to everyone for your reference, specific as follows:

JSON is a widely used format for passing strings, which is easier to understand and easier to manipulate than XML, with two functions, Json_encode () and Json_deconde () in PHP. However, JSON's support for Chinese is not very good, and if you use Json_encode () to handle an array, if there is a Chinese in the arrays, it will be treated as white space.

One way to solve Chinese is to convert Chinese to another encoding format before using Json_encode (), and then decoding the JSON strings. There is another way to get the solution in the new version of PHP, shown in the code below.

The following is a code example

<?php
  Header ("Content-type:text/html;charset=utf-8");
  $arrayName = Array (' City ' => ' Guangdong ', ' goods ' => ' cookies ');
  $arr = Json_encode ($arrayName);
  echo $arr. " </br> ";
  Var_dump (Json_decode ($arr));
  echo "</br>";
  Echo UrlDecode (Json_encode (Ch_json ($arrayName)). " </br> ";
  /* Need PHP version above 5.4
  echo json_encode ($arrayName, json_unescaped_unicode);
*/
  function Ch_json ($arr) {
    if (Is_array ($arr)) {
      foreach ($arr as $key => $value) {
        $arr [ UrlEncode ($key)] = Ch_json ($value);
      }
    } else{return
      UrlEncode ($arr);
    }
    return $arr;
>

PS: Here again for you to recommend a few more practical JSON online tools for your reference to use:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

For more information about PHP interested readers can view the site topics: "PHP JSON format data Operation tips Summary", "PHP for XML file Operation skills Summary", "PHP basic Grammar Introductory Course", "PHP Array" Operation Techniques Encyclopedia, "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"

I hope this article will help you with the PHP program design.

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.