Is there a way to keep
json_encode() from returning
null for a string that contains an invalid (non-UTF-8) character?
It can be a pain in the ass to debug in a complex system. It would be much more fitting to actually see the invalid character, or at least have it omitted. As it stands,
json_encode() will silently drop the entire string.
Example (in UTF-8):
$string = array(utf8_decode("Düsseldorf"), // Deliberately produce broken string "Washington", "Nairobi"); print_r(json_encode($string));
Note: I am not looking to make broken strings work in json_encode(). I am looking for ways to make it easier to diagnose encoding errors. A
null string isn’t helpful for that.