json_encode Chinese display problem solution_PHP tutorial

WBOY
Release: 2016-07-13 10:57:17
Original
1442 people have browsed it

The Chinese display problem of json_encode in PHP is a problem that troubles many programmers. Now I will introduce two solutions to the Chinese display problem for your reference.

Json has become the most commonly used data format in current web development. PHP also supports the json and array conversion functions json_encode and json_decode starting from 5.2. But during use, we will find that (the Chinese character "you" is taken as an example below) the Chinese converted by the json_encode function has all become an encoding similar to u4f60 (you). Although it does not affect program execution, it is very unintuitive

First of all, json_encode converts Chinese into the corresponding hexadecimal representation of unicode code u4f60, (similar to the escape function of js (%u4f60)), that is, 0x4f60. Therefore, we only need to convert the unicode code (UCS-2) into utf-8 encoded Chinese characters. The function is as follows:

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

/**
 * json_encode 支持中文版
 * @param mixed $data 参数和 json_encode 完全相同
 */
function json_encode_cn($data) {
 $data = json_encode($data);
 return preg_replace("/u([0-9a-f]{4})/ie", "iconv('UCS-2', 'UTF-8', pack('H*', ''));", $data);
}

Copy code

/**
* json_encode supports Chinese version
* @param mixed $data parameters are exactly the same as json_encode
​*/
function json_encode_cn($data) {
$data = json_encode($data);
return preg_replace("/u([0-9a-f]{4})/ie", "iconv('UCS-2', 'UTF-8', pack('H*', '$1')); ", $data);
}

 代码如下 复制代码

/**
 * 对数组和标量进行 urlencode 处理
 * 通常调用 wphp_json_encode()
 * 处理 json_encode 中文显示问题
 * @param array $data
 * @return string
 */
function wphp_urlencode($data) {
 if (is_array($data) || is_object($data)) {
  foreach ($data as $k => $v) {
   if (is_scalar($v)) {
    if (is_array($data)) {
     $data[$k] = urlencode($v);
    } else if (is_object($data)) {
     $data->$k = urlencode($v);
    }
   } else if (is_array($data)) {
    $data[$k] = wphp_urlencode($v); //递归调用该函数
   } else if (is_object($data)) {
    $data->$k = wphp_urlencode($v);
   }
  }
 }
 return $data;
}

/**
 * json 编码
 *
 * 解决中文经过 json_encode() 处理后显示不直观的情况
 * 如默认会将“中文”变成"u4e2du6587",不直观
 * 如无特殊需求,并不建议使用该函数,直接使用 json_encode 更好,省资源
 * json_encode() 的参数编码格式为 UTF-8 时方可正常工作
 *
 * @param array|object $data
 * @return array|object
 */
function ch_json_encode($data) {
 $ret = wphp_urlencode($data);
 $ret = json_encode($ret);
 return urldecode($ret);
}

Here, first convert the target data into a unicode encoded json string, then use regular rules to replace the corresponding four letters starting with u with the corresponding text, and then transcode again. The e in the preg_replace regular allows the second parameter to perform the eval operation. First, uxxxx is matched, and then the hexadecimal value xxxx is converted into Unicode-encoded characters through the pack function, and then the Unicode code is converted into utf-8 code, and then You can see normal Chinese characters. Another solution where json_encode() does not support Chinese characters
The code is as follows Copy code
/**
* Urlencode arrays and scalars
* Usually call wphp_json_encode()
* Handle json_encode Chinese display problem
* @param array $data
* @return string
​*/
function wphp_urlencode($data) {
if (is_array($data) || is_object($data)) {
foreach ($data as $k => $v) {
if (is_scalar($v)) {
If (is_array($data)) {
$data[$k] = urlencode($v);
} else if (is_object($data)) {
$data->$k = urlencode($v);
}
} else if (is_array($data)) {
$data[$k] = wphp_urlencode($v); //Call this function recursively
} else if (is_object($data)) {
$data->$k = wphp_urlencode($v);
}
}
}
return $data;
} /**
* json encoding
*
* Solve the problem that the Chinese display after json_encode() processing is not intuitive
* By default, "Chinese" will be changed to "u4e2du6587", which is not intuitive
* If there are no special needs, it is not recommended to use this function. It is better to use json_encode directly and save resources
* json_encode() can only work properly when the parameter encoding format is UTF-8
*
* @param array|object $data
* @return array|object
​*/
function ch_json_encode($data) {
$ret = wphp_urlencode($data);
$ret = json_encode($ret);
return urldecode($ret);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632104.htmlTechArticleThe Chinese display problem of json_encode in php is a problem that troubles many programmers. Let me introduce two problems to you. A solution to the Chinese display problem, you can refer to it. json has become...
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