3

I have a Persian text "سرما"

And then when I convert it to JSON using json_encode(), I get a series of escaped character codes such as \u0633 which seems to be expected and of a rational process. But my confusion lies where I don't know how to convert them back into readable string of characters. How should I do that in PHP?

Should I use anything of mb_* family? I also have checked json_encode() parameters and have found nothing appropriate for me.

UPDATE what I get saved in my DB is: ["u0633u0631u0645u0627"]

Which shows the characters are not escaped properly. While if I change it to ["\u0633\u0631\u0645\u0627"] it becomes easily readable by json_decode()

8
  • 1
    Doesn't json_decode do what you want? Commented Apr 15, 2016 at 1:21
  • No it doesn't, it just give them back as they are. I mean with \u* notation... Commented Apr 15, 2016 at 1:21
  • It works for me. I get back the original string with Persian characters. Commented Apr 15, 2016 at 1:24
  • 1
    See demo: ideone.com/uYwBG4 Commented Apr 15, 2016 at 1:27
  • 1
    It sounds like the problem is with how you're saving to the DB. Why are you converting to JSON when you store in the DB, it should be able to store UTF-8 characters directly without conversion. Commented Apr 15, 2016 at 1:32

3 Answers 3

5

They should be converted back on the other end when it's decoded. This is the safest option as it might not be possible to guaranteed that the transmission or storage will not corrupt a multi-byte encoding.

If you're certain that everything is safe for UTF8 end-to-end you can do:

$res = json_encode($foo, \JSON_UNESCAPED_UNICODE);

http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

2 Comments

You still need to fix your escaping problem when storing in the DB. If it's not escaping backslashes, it's not escaping other special characters, so you're open to SQL injection.
Indeed I'm so aware of SQL-Injection. My case is a special one. I am not even used to store data in DB in an aggregated fashion.
0

Maybe try encoding the unicode characters, and then json_encoding it, then on the other side (receiving JSON) decode the json, then decode the unicode.

Example:

//Encode
json_encode(utf8_encode($string));

//Decode
utf8_decode(json_decode($string)); 

Comments

0

its simple just use JSON_UNESCAPED_SLASHES atribute your problem is't utf8 you need force JSON to don't escape Slashes

example

$bar = "سرما";
$res = json_encode($bar, JSON_UNESCAPED_SLASHES );
// $res equal to ["\u0633\u0631\u0645\u0627"]

if you check the result in your MYSQL Database it happen when you did't Use addslashes() example

$bar = "سرما";
$res = json_encode($bar, JSON_UNESCAPED_SLASHES );
$res = addslashes($res);
// $res equal to ["\\u0633\\u0631\\u0645\\u0627"] now it's ready to use in MYSQL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.