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

问题描述

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

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

解决方法

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

$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

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