Home  >  Article  >  Backend Development  >  json versions below php5.4 do not support the solution of not escaping Chinese content, _PHP tutorial

json versions below php5.4 do not support the solution of not escaping Chinese content, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:41845browse

Json versions below php5.4 do not support the solution of not escaping Chinese content,

The example in this article describes the solution to the problem that json in versions below php5.4 does not support unescaped Chinese content. Share it with everyone for your reference. The specific analysis is as follows:

When writing the ERP interface, I encountered the JAVA side receiving this json_encoded content

Copy the code The code is as follows:
{"orderCode": "1401160935542399","creator":"u751fu6d3bu7528u54c1u6d4bu8bd5u5c0fu5c4b"}

Among them, "creator":"u751fu6d3bu7528u54c1u6d4bu8bd5u5c0fu5c4b" is Chinese, and currently we use UTF8. But when the JAVA side receives this, it automatically converts the escaped Chinese back to Chinese. The signature calculation method is based on this, and the signature will naturally not be correct.

I checked the PHP manual and found that Chinese characters must be escaped under 5.4, but the PHP version on our server is 5.3, so I used PHP to simulate a JSON method.

Copy code The code is as follows:
//Simulate joson Chinese without escaping
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
Function json_encode_ex($var) {
          return json_encode($var, JSON_UNESCAPED_UNICODE);
}
} else {
Function json_encode_ex($var) {
If ($var === null)
              return 'null';

if ($var === true)
              return 'true';

if ($var === false)
              return 'false';

static $reps = array(
              array("\", "/", "n", "t", "r", "b", "f", '"', ),
               array('\\', '\/', '\n', '\t', '\r', '\b', '\f', '"', ),
);

if (is_scalar($var))
                return '"' . str_replace($reps[0], $reps[1], (string) $var) . '"';

if (!is_array($var))
                throw new Exception('JSON encoder error!');

$isMap = false;
         $i = 0;
foreach (array_keys($var) as $k) {
If (!is_int($k) || $i++ != $k) {
                     $isMap = true;
                 break;
            }
}

$s = array();

if ($isMap) {
foreach ($var as $k => $v)
                    $s[] = '"' . $k . '":' . call_user_func(__FUNCTION__, $v);

             return '{' . implode(',', $s) . '}';
         } else {
              foreach ($var as $v)
                     $s[] = call_user_func(__FUNCTION__, $v);

             return '[' . implode(',', $s) . ']';
}
}
}

When using it, just use it as a built-in function. json_encode_ex(array('Diaoyu Island'=>'China')); also supports multi-dimensional arrays.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/942408.htmlTechArticleThe solution for json versions below php5.4 does not support Chinese content without escaping. The example in this article tells the story of php5.4 The following versions of json do not support the solution of not escaping Chinese content. Share it with everyone...
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