When decoding/encoding a utf8 string using json_encode/json_decode I do not get back the string in the original encoding...
$test = '{"c":"limón"}';
echo $test; //=> {"c":"limón"}
echo json_decode($test)->{"c"}; //=> limón
echo json_encode(json_decode($test)); //=> {"c":"lim\u00f3n"}
How can I encode the string back to its original encoding (utf8)?
The default behavior of
json_encode
is to escape all Unicode characters. If your PHP is version 5.4.0 or greater, you can passJSON_UNESCAPED_UNICODE
as the second parameter ofjson_encode
to get the behavior you're expecting. There are numerous hacks to get this behavior in earlier versions, including preprocessing your object to encode Unicode characters as HTML entities, then reversing the transformation afterwards.