jsonpersian

PHP json_encode() function and Persian language


I'm converting a website database to Joomla's K2 component database. in K2 there is a extra_fields columns that user can create custom fields, similar as Drupal's CCK. So I used this feature to keep item's source in a field. {"id":"7", "value":"Text"} but when I use to json_encode "ارتباطات و اطلاع رساني" or anything else instead of getting

{"id":7,"value":"\u0627\u0631\u062a\u0628\u0627\u0637\u0627\u062a \u0648 \u0627\u0637\u0644\u0627\u0639 \u0631\u0633\u0627\u0646\u064a"}

that I see in my localhost, I have:

{"id":"7","value":"u0631u0648u0627u0628u0637 u0639u0645u0648u0645u064a"}, when data is inserted to database

UPDATE:

UPDATE 2:

I think I solved my own problem. check the answer.


Solution

  • Since 5.4.0 you can use unescaped unicode for JSON, by passing the constant JSON_UNESCAPED_UNICODE as second parameter (which of course is optional):

    <?php var_dump(json_encode(array('text' => 'ارتباطات و اطلاع رسانی'), JSON_UNESCAPED_UNICODE)); ?>
    

    output will be:

    string(52) "{"text":"ارتباطات و اطلاع رسانی"}"
    

    (from here)