小编典典

json_encode返回NULL,json_last_error_msg给出“控制字符错误,可能编码错误”

json

读入我的编辑器后,文件看起来很好。

$file = file_get_contents('path/to/file.json');
$json =  json_decode($file, true);
var_dump($json); // null
echo json_last_error_msg(); //Control character error, possibly incorrectly encoded

关于此错误消息的含义,目前没有多少。


阅读 286

收藏
2020-07-27

共1个答案

小编典典

您可以删除控制字符,PCRE支持字符类的POSIX表示法[:cntrl:]

$json = preg_replace('/[[:cntrl:]]/', '', $json);
$json = json_decode($json, true);
var_dump($json);
echo json_last_error_msg();
2020-07-27