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