Yehor
Yehor

Reputation: 6813

converting unicode \u sequence (like \u041a) to normal utf-8 text in php

I have a string like this "\u041a\u043b\u0443\u0431 Test";

It was decoded by json_encode(), the original string was "Клуб Test" in russian. when I put it to js like

alert("\u041a\u043b\u0443\u0431 Test");

I get correct displaying, like on the screen. So js in some way poperly decodes it to normal view. The question is how can I do the same thing in php, is there any built in method?

enter image description here


THE ANSWER IS:
$json_in = '{"testKey":"\u041a\u043b\u0443\u0431 Test"}';
$json_out = json_decode($json_in, true);
or
Convert  "\u041a\u043b\u0443\u0431" to  "Клуб" 
and perform html_entity_decode($str, null, 'UTF-8');

Upvotes: 4

Views: 16247

Answers (2)

Starx
Starx

Reputation: 78991

While converting the data, use JSON_UNESCAPED_UNICODE as the options

echo json_encode($text, JSON_UNESCAPED_UNICODE);

Upvotes: 6

globin
globin

Reputation: 442

You probably want HTML entities to print the characters:

  • Ӓ for decimal code
  • Ī for hex code

Upvotes: 1

Related Questions