PHP把unicode編碼的字串轉為人眼可看的字串
阿新 • • 發佈:2018-11-10
json字串裡面,中文被unicode編碼了,看不出來什麼:
$s = '[{"param_name":"email","param_caption":"\u90ae\u7bb1","operator":"\u5305\u542b","value":"aaaa\u5927\u592b\u6492"}]';
用這個函式可以轉回中文:
/** * 把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']; }
轉換後:
[{"param_name":"email","param_caption":"郵箱","operator":"包含","value":"aaaa大夫撒"}]