search
Homephp教程php手册PHP5.4中json_encode中文转码的变化[让json更懂中文]

在php5.4以前做json_encode的时候中文会被unicode编码,中文都会被编码, 变成不可读的, 类似“\u***”的格式, 还会在一定程度上增加传输的数据量。

 

例如:

 

$str = '中文';

echo json_encode($str);

//"\u4e2d\u6587"

 

php5.4开始

echo json_encode($str, JSON_UNESCAPED_UNICODE);  

//"中文"

php5.4让json更懂中文!

 

5.4之前不进行unicode转码,有3种方法处理:

 

PHP5.4中json_encode中文转码的变化让json更懂中文

 

但其实前两种方式是会导致问题,在一些特殊的情况下。如下例:

 

function myjson($code) {

    $code = json_encode(urlencodeAry($code));

    return urldecode($code);

}

 

function urlencodeAry($data) {

    if(is_array($data)) {

        foreach($data as $key=>$val) {

            $data[$key] = urlencodeAry($val);

        }

        return $data;

    } else {

        return urlencode($data);

    }

}

 

$test = array (

    0 => '"大连周水子机场"→人民路',

    1 => '运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车',

);

 

/*

通过常规则的json_encode|json_decode来编解码

["\"\u5927\u8fde\u5468\u6c34\u5b50\u673a\u573a\"\u2192\u4eba\u6c11\u8def","\u8fd0\u8425\u65f6

\u95f4\uff1a5\uff1a10\uff5e21\uff1a00 \u7968\u4ef7\uff1a16\u5143 \u53d1\u8f66\u95f4\u969420

\u5206\u949f\u4e00\u73ed\uff0c\u5ba2\u6ee1\u968f\u65f6\u53d1\u8f66"]

array (

  0 => '"大连周水子机场"→人民路',

  1 => '运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车',

)

*/

$test1 = json_encode($test);

$test2 = json_decode($test1, TRUE);

echo $test1;

echo PHP_EOL;

var_export($test2);

echo PHP_EOL;

 

/*

通过myjson|json_decode来编解码,但是会出报错json_last_error返回(JSON_ERROR_SYNTAX === 4),

因为""大连周水子机场"→人民路"

[""大连周水子机场"→人民路","运营时间:5:10~21:00 票价:16元 发车间隔20分钟一班,客满随时发车"]

NULL

*/

$test1_1 = myjson($test);

$test2_1 = json_decode($test1_1, TRUE);

echo $test1_1;

echo PHP_EOL;

var_export($test2_1);

echo PHP_EOL;

 

/*

通过json_enco+pack|json_decode来编解码,保证不会对中文编码,但是会缺少运营时间数据和票价

["\"大连周水子机场\"→人民路","运营时间::~: 票价:元 发车间隔分钟一班,客满随时发车"]

array ( www.2cto.com

  0 => '"大连周水子机场"→人民路',

  1 => '运营时间::~: 票价:元 发车间隔分钟一班,客满随时发车',

)

*/

function replaceUni($str) {

    return preg_replace("#\\\u([0-9a-f]+)#ie", "iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", $str);

}

$test1_2 = replaceUni(json_encode($test));

$test2_2 = json_decode($test1_2, TRUE);

echo $test1_2;

echo PHP_EOL;

var_export($test2_2);

echo PHP_EOL;

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool