Home > Backend Development > PHP Tutorial > How json_encode prevents Chinese characters from being escaped into unicode

How json_encode prevents Chinese characters from being escaped into unicode

WBOY
Release: 2016-07-29 09:02:09
Original
1051 people have browsed it

As we all know, json_encode usually escapes Chinese characters in json into unicode, but this is not necessarily what we want. Sometimes, we need to obtain a json string in the form of Chinese characters, for example, we need to obtain a gbk-encoded json string (as long as the string in the form of Chinese characters is transcoded). Is there any good way?

php officials heard this need and provided a reliable solution: JSON_UNESCAPED_UNICODE. This parameter can ensure that json_encode will no longer convert Chinese characters into unicode.

It seems like this solves the problem? When we happily used this parameter, we found that it was of no use. A closer look shows that this parameter is only supported by PHP after 5.4. What about the earlier PHP?

The community provides a solution:

<span>1</span><span>function</span> my_json_encode(<span>$arr</span><span>){
</span><span>2</span><span>//</span><span>convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding</span><span>3</span><span>array_walk_recursive</span>(<span>$arr</span>, <span>function</span> (&<span>$item</span>, <span>$key</span>) { <span>if</span> (<span>is_string</span>(<span>$item</span>)) <span>$item</span> = mb_encode_numericentity(<span>$item</span>, <span>array</span> (0x80, 0xffff, 0, 0xffff), 'UTF-8'<span>); });
</span><span>4</span><span>return</span> mb_decode_numericentity(json_encode(<span>$arr</span>), <span>array</span> (0x80, 0xffff, 0, 0xffff), 'UTF-8'<span>);
</span><span>5</span> }
Copy after login

However, this method is only supported by 5.3, because 5.2 does not support anonymous functions. As for the solution? Just define the anonymous function.

The above introduces how json_encode prevents Chinese characters from being escaped into unicode, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template