Home > Backend Development > PHP Tutorial > Solution to not escaping Chinese when using json_encode function in PHP, _PHP tutorial

Solution to not escaping Chinese when using json_encode function in PHP, _PHP tutorial

WBOY
Release: 2016-07-13 10:14:08
Original
838 people have browsed it

Solution to not escaping Chinese characters when using json_encode function in PHP,

The example in this article describes the solution of not escaping Chinese when using the json_encode function in PHP. Share it with everyone for your reference. The specific method is as follows:

The json_encode function will not convert Chinese characters in gbk or directly convert them into spaces. This article will give you some tips on how to deal with json unescaped Chinese problems. I believe it will be helpful to everyone.

If you call the json_encode() function that comes with PHP, when you encounter Chinese characters, the Chinese characters will be escaped. For example:

Copy code The code is as follows:
echo json_encode(array('Hello'));
// Output: ["u4f60u597d"]

This is very annoying, like a bunch of gibberish. The JSON standard never says to escape non-ASCII characters. The standard says "Any UNICODE character".
How to disable this escaping? The answer is that PHP's own json_encode() cannot disable this feature (before version 5.4.0, in later versions you can add the JSON_UNESCAPED_UNICODE option), you can only change to a new JSON library . For the sake of simplicity, I simply wrote dozens of lines of code to implement a json_encode().
Copy code The code is as follows:
class Util
{
​ static function json_encode($input){
​​​​ // Starting from PHP 5.4.0, this option is added.
If(defined('JSON_UNESCAPED_UNICODE')){
               return json_encode($input, JSON_UNESCAPED_UNICODE);
}
           if(is_string($input)){
                $text = $input;
                $text = str_replace('\', '\\', $text);
               $text = str_replace(
                array("r", "n", "t", """),
                  array('r', 'n', 't', '\"'),
                      $text);
               return '"' . $text . '"';
            }else if(is_array($input) || is_object($input)){
               $arr = array();
$is_obj = is_object($input) || (array_keys($input) !== range(0, count($input) - 1));
foreach($input as $k=>$v){
If($is_obj){
                               $arr[] = self::json_encode($k) . ':' . self::json_encode($v);
                    }else{
                                 $arr[] = self::json_encode($v);
                }
            }
                if($is_obj){
                        return '{' . join(',', $arr) . '}';
               }else{
Return '[' . join(',', $arr) . ']';
            }
         }else{
                   return $input . '';
}
}
}

For things that are not considered, such as judging associative arrays (is_obj), let's talk about it when we encounter problems. If you don't like classes, just convert it into a pure function yourself and change the name.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/910599.htmlTechArticleThe solution to not escaping Chinese when PHP uses the json_encode function. This article describes the example of PHP not escaping when using the json_encode function. Chinese solution. Share it with everyone for your reference. Tool...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template