Table of Contents
The solution to the problem that json in versions below php5.4 does not support unescaped content in Chinese
Home Backend Development PHP Tutorial JSON versions below php5.4 do not support Chinese solutions without escaping content_PHP Tutorial

JSON versions below php5.4 do not support Chinese solutions without escaping content_PHP Tutorial

Jul 13, 2016 am 10:01 AM
json php5.4 No Chinese content method Version of solve escape

The solution to the problem that json in versions below php5.4 does not support unescaped content in Chinese

This article mainly introduces the solution that json in versions below php5.4 does not support unescaped content in Chinese The solution is to simulate joson Chinese without escaping through a custom php method, which has certain reference value. Friends in need can refer to it

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

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 those below 5.4 must escape Chinese, but the PHP version on our server is 5.3, so we used PHP to simulate a JSON method.

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 directly 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/971924.htmlTechArticleThe solution for json in versions below php5.4 does not support Chinese content without escaping. This article mainly introduces php5. Versions below 4 of json do not support the solution of not escaping Chinese content, through a custom...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel.

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts)

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

The difference between Go language methods and functions and analysis of application scenarios The difference between Go language methods and functions and analysis of application scenarios Apr 04, 2024 am 09:24 AM

The difference between Go language methods and functions and analysis of application scenarios

750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth 750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth Apr 23, 2024 pm 03:28 PM

750,000 rounds of one-on-one battle between large models, GPT-4 won the championship, and Llama 3 ranked fifth

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization tips for converting PHP arrays to JSON

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

See all articles