When I run the following code json_encode() returns false (error). Does anyone know why?
$r = json_encode(chr(128));
var_dump($r);
The result is:
bool(false)
When I run the following code json_encode() returns false (error). Does anyone know why?
$r = json_encode(chr(128));
var_dump($r);
The result is:
bool(false)
var_dump(chr(128));
The chr(128)
is a malformed UTF-8 character. json_encode()
can't encode malformed UTF-8 characters.
https://datatracker.ietf.org/doc/html/rfc8259#section-1
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
A string is a sequence of zero or more Unicode characters
json_encode()
with flags.If you are running PHP 7.3 or newer, you can try this code to throw the error message.
var_dump(json_encode(chr(128), JSON_THROW_ON_ERROR));
Result:
Fatal error: Uncaught JsonException: Malformed UTF-8 characters, possibly incorrectly encoded.
To prevent the errors, use JSON_INVALID_UTF8_IGNORE
flag but this still can not encode invalid characters.
Example:
var_dump(json_encode(chr(128), JSON_INVALID_UTF8_IGNORE));
Result:
string '""' (length=2)
You may clean up the string to remove any invalid/malformed characters with this code.
$string = chr(128);
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
// then encode
var_dump(json_encode($string));// result is string '"?"' (length=3)