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

How json_encode prevents Chinese characters from being escaped into unicode, json_encodeunicode_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:58:32788browse

How json_encode prevents Chinese characters from being escaped into unicode, json_encodeunicode

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.

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> }

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1102853.htmlTechArticleHow json_encode prevents Chinese characters from being escaped into unicode, json_encodeunicode As we all know, json_encode usually escapes Chinese characters in json into unicode , but this is not necessarily what we want. ...
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