23

I'm trying to encode Cyrillic UTF-8 array to JSON string using php's function json_encode. The sample code looks like this:

<?php
  $arr = array(
     'едно' => 'първи',
     'две' => 'втори'
  );
  $str = json_encode($arr);
  echo $str;
?>

It works fine but the result of the script is represented as:

{"\u0435\u0434\u043d\u043e":"\u043f\u044a\u0440\u0432\u0438","\u0434\u0432\u0435":"\u0432\u0442\u043e\u0440\u0438"}

which makes 6 characters for each Cyrillic character. Is there a way to get the original characters for key/value pairs instead of encoded ones?

2
  • As you echo the string, I assume this wents into a HTTP response. Which encoding is that response using? For valid character encodings please see as well json.org.
    – hakre
    Commented Jun 2, 2012 at 18:11
  • 3
    hi try this $str = json_encode($json,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
    – Priya Bose
    Commented Jul 12, 2015 at 11:26

8 Answers 8

31

Can't you use JSON_UNESCAPED_UNICODE constant here?

1
  • This is itroduced in php 5.4.0. Looks like it should work but still cannot confirm.
    – AquilaX
    Commented Sep 28, 2011 at 8:53
15
$str = json_encode($arr, JSON_UNESCAPED_UNICODE);

The use of this solution worked for me with the Latin and the Cyrillic alphabet, with PHP 5.5

10

I found this in the code of Zend framework:

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php

Take a look at the function decodeUnicodeString ( line 474 ):

 /**
     * Decode Unicode Characters from \u0000 ASCII syntax.
     *
     * This algorithm was originally developed for the
     * Solar Framework by Paul M. Jones
     *
     * @link   http://solarphp.com/
     * @link   http://svn.solarphp.com/core/trunk/Solar/Json.php
     * @param  string $value
     * @return string
     */
    public static function decodeUnicodeString($chrs)

It's static, and you can easily extract it - just replace the line:

490:           $utf8 .= self::_utf162utf8($utf16);

with:

490:           $utf8 .= mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');

Not an ideal solution, but did the job for me :o)

0
3

This is a realy old question, but I don't think it was answered correctly.

use something like this:

print json_encode($array, JSON_UNESCAPED_UNICODE);
3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Rahul
    Commented Feb 21, 2017 at 11:43
  • @rahul_m where is the link here? Commented Feb 21, 2017 at 11:46
  • I don't find relevant category for this answer, so I mapped with this category.
    – Rahul
    Commented Feb 21, 2017 at 11:47
2

It worked with http://pear.php.net/pepr/pepr-proposal-show.php?id=198

With nasty bypass in JSON.php, rows 298..

$char = pack('C*', $ord_var_c, ord($var{$c + 1}));
$c += 1;
//$utf16 = $this->utf82utf16($char);
//$ascii .= sprintf('\u%04s', bin2hex($utf16));
$ascii .= $char;

Thanks!

2
  • How did you find out about that bypass? Commented May 16, 2011 at 0:58
  • Figured it aut by myself. It was obvious
    – AquilaX
    Commented Jun 2, 2011 at 9:32
2

It looks like PHP's built in json_encode only works with UTF-8, and no other bells and whistles for tweaking how it works with respect to encoding.

I found A completely fair and balanced comparison of php json libraries on Google. It might help you. You might try another library based on the tables here, if possible. There are additional PHP libraries listed at json.org that you can experiment with.

1

I was dealing the same problem for Turkish ... indeed we don't have to do anything browsers automatically converts them in JS code blocks. So the easiest way of getting them decoded is getting them through javascript. (Ajax etc...)

Json encode for with non ascii characters ?

1

You can use this options:

json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

Not the answer you're looking for? Browse other questions tagged or ask your own question.