Mohamed Said Mohamed Said - 1 year ago 97
JSON Question

Arabic Characters in JSON decoding

$test = json_encode('بسم الله');
echo $test;

As a result of this code, the output is:
"\u0628\u0633\u0645 \u0627\u0644\u0644\u0647"
while it should be something like "بسم الله". Arabic Characters are encoded when being JSON encoded while at the Youtube API this is not the case:

You can see at Youtube that Arabic characters are displayed properly. What could be my mistake?

HINT: I'm working on an API< the example is just for the sake of clarification.

Answer Source

"\u0628\u0633\u0645 \u0627\u0644\u0644\u0647" and "بسم الله" are equivalent in JSON.

PHP just defaults to using Unicode escapes instead of literals for multibyte characters.

You can specify otherwise with JSON_UNESCAPED_UNICODE (providing you are using PHP 5.4 or later).

json_encode('بسم الله', JSON_UNESCAPED_UNICODE);

(Note: Untested because I don't have convenient access to a new enough PHP).