search
Homephp教程php手册Json_encode防止汉字转义成unicode的方法,json_encodeunicode

Json_encode防止汉字转义成unicode的方法,json_encodeunicode

大家都知道,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.

<&#63;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);
}

您可能感兴趣的文章:

  • php json_encode奇怪问题说明
  • PHP学习散记_编码(json_encode 中文不显示)
  • php中json_decode()和json_encode()的使用方法
  • PHP5.4中json_encode中文转码的变化小结
  • php json_encode值中大括号与花括号区别
  • php数组转换js数组操作及json_encode的用法详解
  • 浅析php中json_encode()和json_decode()
  • php中json_encode处理gbk与gb2312中文乱码问题的解决方法
  • PHP使用json_encode函数时不转义中文的解决方法
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)