As I notice PHP's json_encode($array) mess things up on diacritics. If I update my database column type text with javascript-created JSON passed over HTTP, everything looks good. but when I create the JSON into PHP, some characters get encoded weirdly.
I have this array;
$array = ['M-am întîlnit ieri cu','fosta mea profă de matematică'];
$text = implode(",",$array); // this looks good in db
$json = json_encode($array); // this don't and returns error when try to decode later.
As I notice PHP's json_encode($array) mess things up on diacritics. If I update my database column type text with javascript-created JSON passed over HTTP, everything looks good. but when I create the JSON into PHP, some characters get encoded weirdly.
I have this array;
$array = ['M-am întîlnit ieri cu','fosta mea profă de matematică'];
$text = implode(",",$array); // this looks good in db
$json = json_encode($array); // this don't and returns error when try to decode later.
Share
Improve this question
asked Feb 14, 2022 at 3:20
Mihai GalanMihai Galan
4566 silver badges13 bronze badges
4
- "PHP's json_encode($array) mess things up on diacritics" - I cannot reproduce it. The problem you have, whatever it is, is probably in some other part of the application. – Álvaro González Commented Feb 14, 2022 at 8:24
- @ÁlvaroGonzález . On print, it looks good. but when saved on the database it looked way different. Also when returning the JSON string from the database I got UTF-8 error on decoding. – Mihai Galan Commented Feb 14, 2022 at 16:30
-
If you have an encoding issue somewhere along your stack, I can't see how adding the
JSON_UNESCAPED_UNICODE
flag will help.json_encoded()
produces US-ASCII by default and that flag forces pure UTF-8 (demo). – Álvaro González Commented Feb 14, 2022 at 16:33 - In my case, It looks exactly like your example but in DB, and only if I don't add the JSON_UNESCAPED_UNICODE. This option actually solved the issue for me. I'm using PHP 7.0 if it counts. – Mihai Galan Commented Feb 15, 2022 at 0:01
2 Answers
Reset to default 7First off, it's worth pointing out that PHP is not "messing up" anything. It's escaping characters, which may look weird, but it is perfectly valid and when you json_decode
it later it will be just the same as it was originally. See here: https://3v4l/Smj2F
If you don't like the escaping though, you can use the JSON_UNESCAPED_UNICODE
flag:
https://www.php/function.json-encode
This flag will "encode multibyte Unicode characters literally" according to https://www.php/manual/en/json.constants.php.
So you can do:
json_encode($array, JSON_UNESCAPED_UNICODE);
And it will give you the following output:
["M-am întîlnit ieri cu","fosta mea profă de matematică"]
Working example: https://3v4l/daETG
For those looking for exact equivalent result PHP json_encode()
v.s. JavaScript JSON.stringify()
as asked in question you must use at least these 2 flags
json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
because of for example JS produces "https://lh3.googleusercontent."
but PHP without JSON_UNESCAPED_SLASHES
produces "https:\/\/lh3.googleusercontent."
. Is important in my case because of Im creating sha hash of both and pare it. If frontend hash is the same as backend hash it means data are synced and no traffic/syncing is needed. Im not sure if some other flag is needed.