1

How to get utf-8 in json_encode php when convert from array to string ?

This is my code for test.php:

<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<?PHP
session_start();
include("connect.php");
$word = mysqli_real_escape_string($db_mysqli,$_GET['word']);
$xx = array();
$xx[] = $word;
$xx[] = $word;
$xx_1 = json_encode($xx);
print_r($xx_1);
?>

When test load

example.com/test.php?word=神州

It's show

["\u795e\u5dde","\u795e\u5dde"]

I want to show like this

["神州","神州"]
Aftab H.
  • 1,517
  • 4
  • 13
  • 25
mamiw
  • 123
  • 4
  • 9

2 Answers2

2
$xx_1 = json_encode($xx, JSON_UNESCAPED_UNICODE);
print_r($xx_1);.

works for php5.4+

Actually ["\u795e\u5dde"] is same as ["神州"]. most of the time you json_encode the data before storing/transferring data around and after that you have to json_decode the value and process it, the json_docode will take care of the unicodification. But the above code should give you what you needed.

Doc Link

Shobi
  • 10,374
  • 6
  • 46
  • 82
0

http://php.net/manual/en/mysqli.set-charset.php

$db_mysqli->set_charset('utf8');
indianiso1
  • 64
  • 4