PHP function json_encode () and Persian language

I am converting a site database into a database of Joomla K2 components. K2 has extra_fields columns that the user can create custom fields similar to the Drupal CCK. So I used this function to keep the source of the source in the field. {"id":"7", "value":"Text"} but when I use json_encode "ارتباطات و اطلاع رساني" or something 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"}

which I see in my local host, I:

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

UPDATE:

  • There are no slashes in that I'm trying to use json_encode

  • I wrote php code to read from a table and then converted the data and pasted it into another table

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

     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 problem. check the answer.

+4
source share
3 answers

I assume that the problem was when the data was inserted into mysql, so I used the mysql_real_escape_string () function, and the problem seems to be solved.

-2
source

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

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

the output will be:

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

(from here )

+7
source

I think you should try to isolate where the problem really is. Do you save this in a file? Database? Could it be that slashes are removed using some kind of escaping (or non-insulating) code?

As of PHP 5.3.4, this code:

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

Correctly issues:

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

+1
source

Source: https://habr.com/ru/post/1383147/


All Articles