Problem with Jars_encode Charset

When I use json_encode to encode my multilingual strings, it also changes special characters. What to do to keep them the same.

for instance

<? echo json_encode(array('şüğçö')); 

It returns something like ["\ u015f \ u00fc \ u011f \ u00e7 \ u00f6"]

But I want ["şüğçö"]

+4
source share
7 answers

try:

 <? echo json_encode(array('şüğçö'), JSON_UNESCAPED_UNICODE); 
+3
source

In JSON, any character in strings can be represented by Unicode escape sequences. Thus, "\u015f\u00fc\u011f\u00e7\u00f6" semantically equal to "şüğçö" .

Although this character can also be used in the same way, json_encode probably prefers Unicode escape sequences to avoid character encoding problems.

+2
source
  • You don't have to
  • This is definitely possible even without PHP 5.4.

First use json_encode() to encode the string and store it in a variable.

Then just use preg_replace() to replace all \ uxxxx with unicode again.

+2
source

json_encode() does not provide any parameters for choosing the encoding in which the encoding is located, in versions prior to 5.4.

+1
source

PHP 5.4 adds the JSON_UNESCAPED_UNICODE option, which does what you want. Note that json_encode always outputs UTF-8.

+1
source
 <?php print_r(json_decode(json_encode(array('şüğçö')))); /* Array ( [0] => şüğçö ) */ 

So, do you really need to save these characters in Unscape in JSON?

0
source

Charset solution for Json_encode for PHP 5.3.3

Since JSON_UNESCAPED_UNICODE does not work in PHP 5.3.3, so we used this method and it works.

 $data = array( 'text' => 'Päiväkampanjat' ); $json_encode = json_encode($data); var_dump($json_encode); // text: "P\u00e4iv\u00e4kampanjat" $unescaped_data = preg_replace_callback('/\\\\u(\w{4})/', function ($matches) { return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8'); }, $json_encode); var_dump($unescaped); // text is unescaped -> Päiväkampanjat 
0
source

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


All Articles