Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP json_encode() function and Persian language

Tags:

json

persian

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:

  • There is no slashes in what I'm trying to json_encode

  • I have wrote a php code to read from a table and then convert data and insert them in another table

  • script which I use to create json_encoded data from my source is:

    if($this->source[$i]){
            $this->source[$i] = trim($this->source[$i]);
            $this->extrafield[$i] = array("id"=>"7", "value"=>$this->source[$i]);
            $this->extrafield[$i] = json_encode($this->extrafield[$i]);
    }
    

UPDATE 2:

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

like image 426
Farid Rn Avatar asked Dec 21 '22 05:12

Farid Rn


2 Answers

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)

like image 77
MortezaE Avatar answered Jan 06 '23 06:01

MortezaE


I think you should try to isolate where the issue actually is. Are you storing this in a file? Database? Could it be that the slashes are being removed by some escaping (or unescaping) code?

With PHP 5.3.4, this code:

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

Correctly outputs:

string '{"text":"\u0631\u062a\u0628\u0627\u0637\u0627\u062a \u0648 \u0627\u0637\u0644\u0627\u0639 \u0631\u0633\u0627\u0646\u064a"}' (length=122)

like image 32
Overflowed Avatar answered Jan 06 '23 08:01

Overflowed