Home > Backend Development > PHP Tutorial > PHP JSON_DECODE/JSON_ENCODE Chinese content is NULL or garbled_PHP tutorial

PHP JSON_DECODE/JSON_ENCODE Chinese content is NULL or garbled_PHP tutorial

WBOY
Release: 2016-07-13 10:56:59
Original
1200 people have browsed it

Many friends may encounter problems with NULL or garbled characters when using the functions JSON_DECODE/JSON_ENCODE that comes with PHP to process Chinese content when using json data. Let me introduce to you why such problems occur.

Example

The code is as follows Copy code
 代码如下 复制代码

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

输出结果

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));
代码如下 复制代码

$json = '{"a":"中国人人"}';

var_dump(json_decode($json));

?>

结果

{"text":null,"status":1}

?>

Output results

 代码如下 复制代码

/*
    字符串GBK转码为UTF-8,数字转换为数字。
*/
function ct2($s){
    if(is_numeric($s)) {
        return intval($s);
    } else {
        return iconv("GBK","UTF-8",$s);
    }
}
/*
    批量处理gbk->utf-8
*/
function icon_to_utf8($s) {

  if(is_array($s)) {
    foreach($s as $key => $val) {
      $s[$key] = icon_to_utf8($val);
    }
  } else {
      $s = ct2($s);
  }
  return $s;

}

echo json_encode(icon_to_utf8("厦门"));

object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
} array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
} Absolutely correct without any problems, then let’s test Chinese
The code is as follows Copy code
$json = '{"a":"中国人人"}';<🎜> <🎜>var_dump(json_decode($json));<🎜> <🎜>?> Results {"text":null,"status":1}
Later I learned from the PHP manual that json_encode and json_decode only support utf-8 encoded characters. GBK characters must be converted if you want to use json, so we can handle it easily Convert an encoding
The code is as follows Copy code
/*
​ GBK string transcoding is converted to UTF-8, and numbers are converted to numbers.
*/
function ct2($s){
If(is_numeric($s)) {
           return intval($s);
} else {
         return iconv("GBK","UTF-8",$s);
}
}
/*
Batch processing gbk->utf-8
*/
function icon_to_utf8($s) { if(is_array($s)) {
foreach($s as $key => $val) {
$s[$key] = icon_to_utf8($val);
}
} else {
         $s = ct2($s);
}
Return $s; } echo json_encode(icon_to_utf8("Xiamen"));

This still sometimes causes problems. Then I found a way to use urlencode() to process all the contents of all arrays before json_encode, then use json_encode() to convert it into a json string, and finally use urldecode() Convert the encoded Chinese back.

Write a function

The code is as follows
 代码如下 复制代码

/**************************************************************
*
*    使用特定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)
{
    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]);
            }
        }
    }
}

/**************************************************************
*
*    将数组转换为JSON字符串(兼容中文)
*    @param    array    $array        要转换的数组
*    @return string        转换得到的json字符串
*    @access public
*
*************************************************************/
function JSON($array) {
    arrayRecursive($array, 'urlencode', true);
    $json = json_encode($array);
    return urldecode($json);
}

Copy code
/***************************************************** **********
*
* Use a specific function to process all elements in the array
* @param string &$array The string to be processed
* @param string $function The function to be executed
* @return boolean $apply_to_keys_also Whether to also apply to keys
* @access public
*
*************************************************** ***********/
function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
{
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]);
             }
         }
}
}

http://www.bkjia.com/PHPjc/632125.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/632125.htmlTechArticleMany friends may encounter it when using json data using the function JSON_DECODE/JSON_ENCODE that comes with php to process Chinese content. NULL or garbled characters occur. Let me introduce to you why...
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