Home  >  Article  >  Backend Development  >  How to solve the problem of php json_encode garbled characters

How to solve the problem of php json_encode garbled characters

藏色散人
藏色散人Original
2020-07-09 12:36:353723browse

php json_encode garbled solution: first change the database encoding and page encoding to UTF8; then use "json_encode" to convert the array array in PHP into a JSON string; finally use the function urlencode to process it .

How to solve the problem of php json_encode garbled characters

PHP json_encode Chinese garbled solution

I believe many people use Ajax to interact with the background php page I have all encountered the problem of garbled Chinese characters. As a lightweight data exchange format, JSON is very popular. However, using PHP as the background interaction is prone to the problem of Chinese garbled characters. JSON is the same as js. The characters on the client are processed in the form of UTF8. That is to say, when using JSON as the data format for submission and reception, the characters are encoded in UTF8. When our page encoding and database encoding are not When using UTF8, it is extremely easy for Chinese garbled characters to appear. The solution is naturally to use UTF8 when processing JSON data with js or PHP.

PHP5.2 or above uses json_encode as a built-in function, which brings great convenience to website makers. However, we must note that json_encode only supports UTF8-encoded characters. Otherwise, Chinese characters will be garbled. Or a null value appears.

The solution is divided into the following two steps.

Step1

Ensure that characters are encoded in UTF8 when processing JSON. Specifically, we can change both the database encoding and page encoding to UTF8. Of course, if you like to use gbk encoding, you can convert the characters to UTF8 before JSON processing. There are the following methods in PHP:

 <?php  
      $data="JSON中文";  
      $newData=iconv("GB2312","UTF-8//IGNORE",$data);  
      echo $newData;  
      //ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符都不会被保存。  
      //或是("GB2312","UTF-8",$data);  
 ?>

Step2

The background PHP page (the page is encoded as UTF-8 or the characters have been converted to UTF-8) uses json_encode to convert the array array in PHP is a JSON string. For example:

 <?php  
     $testJSON=array(&#39;name&#39;=>&#39;中文字符串&#39;,&#39;value&#39;=>&#39;test&#39;);  
    echo json_encode($testJSON);  
 ?>

View the output result as:

{“name”:”\u4e2d\u6587\u5b57\u7b26\u4e32″,”value”:”test”}

It can be seen that even if UTF8-encoded characters are used, Chinese garbled characters appear when using json_encode. The solution is to process the characters with the function urlencode() before using json_encode, then json_encode, and use the function urldecode() to convert them back when outputting the result. The details are as follows:

 <?php  
     $testJSON=array(&#39;name&#39;=>&#39;中文字符串&#39;,&#39;value&#39;=>&#39;test&#39;);  
     //echo json_encode($testJSON);  
     foreach ( $testJSON as $key => $value ) {  
         $testJSON[$key] = urlencode ( $value );  
     }  
     echo urldecode ( json_encode ( $testJSON ) );  
 ?>

Check the output result as:

{“name”:”中文字符串”,”value”:”test”}

At this point, Chinese characters are successfully output. Feel free to use json_encode. In this way, the JSON string output in the PHP background will not appear Chinese garbled when eval is received by Ajax in the front-end javascript, because js also processes JSON format data in the form of UTF8, which is similar to PHP, so it receives the PHP page The JSON string does not cause problems.

For more related articles, please visit PHP Chinese website!

The above is the detailed content of How to solve the problem of php json_encode garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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