json_encode returns NULL, json_last_error_msg gives “Control character error, possibly incorrectly encoded”

前端 未结 1 920
遇见更好的自我
遇见更好的自我 2020-12-18 03:29

The file looks fine when read into my editor.

$file = file_get_contents(\'path/to/file.json\');
$json =  json_decode($file, true);
var_dump($json); // null
e         


        
相关标签:
1条回答
  • 2020-12-18 03:56

    you can remove the control character, PCRE supports the POSIX notation for character classes [:cntrl:]

    $json = preg_replace('/[[:cntrl:]]/', '', $json);
    $json = json_decode($json, true);
    var_dump($json);
    echo json_last_error_msg();
    
    0 讨论(0)
提交回复
热议问题