Home >Backend Development >PHP Problem >How to convert json unicode to Chinese in php
php method to convert json unicode to Chinese: 1. Use the "json_encode($log['result_data'],JSON_UNESCAPED_UNICODE);" method to convert; 2. Use "function unicodeDecode($unicode_str){.. .}" method to convert.
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
How to convert json unicode to Chinese in php ?
PHP converts unicode encoded json string to Chinese
Chinese in json is encoded
$s = '[{"param_name":"email","param_caption":"\u90ae\u7bb1","operator":"\u5305\u542b","value":"aaaa\u5927\u592b\u6492"}]';
Convert Chinese encoding to Chinese
Method 1.
json_encode($log['result_data'],JSON_UNESCAPED_UNICODE);
Method 2.
/** * 把unicode编码的字符串转为人眼可看的字符串 * @param $unicode_str * * @return string */ function unicodeDecode($unicode_str){ $unicode_str = str_replace('"', '\"', $unicode_str); $unicode_str = str_replace("'", "\'", $unicode_str); $json = '{"str":"'.$unicode_str.'"}'; $arr = json_decode($json,true); if(empty($arr)){ return ''; } return $arr['str']; }
Result:
[{"param_name":"email","param_caption":"邮箱","operator":"包含","value":"aaaa大夫撒"}]
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert json unicode to Chinese in php. For more information, please follow other related articles on the PHP Chinese website!