


Json_encode method to prevent Chinese characters from escaping into unicode
大家都知道,json_encode通常会把json中的汉字转义成unicode,但是这并不一定是我们想要的。有时候,我们需要获得汉字形式的json字符串,比如需要获得gbk编码的json字符串(只要把汉字形式的字符串转码就可以得到了)。有什么好办法么?
php官方听到了这个需求,并提供了一种可靠的解决方案:JSON_UNESCAPED_UNICODE。这个参数可以保证json_encode不再将汉字转为unicode。
似乎这样就解决了?当我们高高兴兴的用这个参数的时候,发现并没有什么卵用。仔细一看,这个参数只有5.4之后的php支持。那更早期的php怎么办呢?
社区提供了一种方案:
function my_json_encode($arr){ //convmap since x char codes so it takes all multibyte codes (above ASCII ). So such characters are being "hidden" from normal json_encoding array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (x, xffff, , xffff), 'UTF-'); }); return mb_decode_numericentity(json_encode($arr), array (x, xffff, , xffff), 'UTF-'); }
不过这种方法只有5.3才支持,因为5.2并不支持匿名函数。至于解决办法?把匿名函数定义一下即可。
ps:解决json_encode中文UNICODE转码问题
用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式,如果想汉字不进行转码,这里提供三种方法
1.升级PHP,在PHP5.4, 这个问题终于得以解决, Json新增了一个选项: JSON_UNESCAPED_UNICODE, 故名思议, 就是说, Json不要编码Unicode.
<?php echo json_encode("中文", JSON_UNESCAPED_UNICODE); //"中文"
2.把汉字先urlencode然后再使用json_encode,json_encode之后再次使用urldecode来解码,这样编码出来的json数组中的汉字就不会出现unicode编码了。
$array = array( 'test'=>urlencode("我是测试") ); $array = json_encode($array); echo urldecode($array); //{"test":"我是测试"}
3.对unicode码再进行解码,解码函数如下:
function decodeUnicode($str) { return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' ), $str); }
以上就介绍了Json_encode防止汉字转义成unicode的方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
