Home  >  Article  >  Backend Development  >  Detailed explanation of PHP json_encode() function and Chinese garbled code problem, _PHP tutorial

Detailed explanation of PHP json_encode() function and Chinese garbled code problem, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:05:24925browse

Detailed explanation of PHP json_encode() function and Chinese garbled problem,

Use json_encode() built-in function in php (php > 5.2), the data in php can be used with other The language is delivered well and used.

The function of this function is to convert numerical values ​​into json data storage format.

<&#63;php
$arr = array
  (
   'Name'=>'希亚',
   'Age'=>
  );
$jsonencode = json_encode($arr);
echo $jsonencode;
&#63;>

The results of running the program are as follows:

{"Name":null,"Age":}

Chinese characters in the json_encode function are encoded as null. After searching on Google, it is very simple. In order to closely integrate with the front-end, Json only supports utf-encoding. I think it is because the front-end Javascript is also utf-.

<&#63;php
$array = array
 (
  'title'=>iconv('gb','utf-','这里是中文标题'),
  'body'=>'abcd...'
 );
echo json_encode($array);
&#63;>

The result of running this program is:

{"title":"u8fd9u91ccu662fu4e2du6587u6807u9898","body":"abcd..."}

All Chinese characters in the array are missing or u2353 etc. appear after json_encode.

The solution is to use the urlencode() function to process the following. Before json_encode, use urlencode() to process all the contents of all arrays, then use json_encode() to convert it into a json string, and finally use urldecode() to Convert the encoded Chinese back.

<&#63;php
/**************************************************************
 *
 * 使用特定function对数组中所有元素做处理
 * @param string &$array  要处理的字符串
 * @param string $function 要执行的函数
 * @return boolean $apply_to_keys_also  是否也应用到key上
 * @access public
 *
 *************************************************************/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
 static $recursive_counter = ;
 if (++$recursive_counter > ) {
  die('possible deep recursion attack');
 }
 foreach ($array as $key => $value) {
  if (is_array($value)) {
   arrayRecursive($array[$key], $function, $apply_to_keys_also);
  } else {
   $array[$key] = $function($value);
  }
  if ($apply_to_keys_also && is_string($key)) {
   $new_key = $function($key);
   if ($new_key != $key) {
    $array[$new_key] = $array[$key];
    unset($array[$key]);
   }
  }
 }
 $recursive_counter--;
}
/**************************************************************
 *
 * 将数组转换为JSON字符串(兼容中文)
 * @param array $array  要转换的数组
 * @return string  转换得到的json字符串
 * @access public
 *
 *************************************************************/
function JSON($array) {
 arrayRecursive($array, 'urlencode', true);
 $json = json_encode($array);
 return urldecode($json);
}
$array = array
  (
   'Name'=>'希亚',
   'Age'=>
  );
echo JSON($array);
&#63;>

This time it was successful, the results are as follows:

{"Name":"Shia","Age":"20"}

The following will introduce to you the solution to Chinese garbled characters in PHP json_encode

I believe many people have encountered the problem of Chinese garbled characters when using Ajax to interact with the background php page. 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. Client characters 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 garbled characters or empty values ​​will appear. Just appeared.

The solution is divided into the following two steps.

Step1

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

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

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 into a JSON string. For example:

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

View the output result:

{“name”:”u4e2du6587u5b57u7b26u4e32″,”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:

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

View the output result:

{"name":"Chinese string","value":"test"}

At this point, Chinese characters have been 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1068832.htmlTechArticlePHP json_encode() function detailed explanation and Chinese garbled problem, use json_encode() built-in function in php (php 5.2) The data in php that can be used can be transferred well with other languages ​​​​and make...
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