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

["神州","神州"]

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

With this code it's not show anything $xx_1 = json_encode($xx, JSON_UNESCAPED_UNICODE); print_r($xx_1);.
i don't know why its like this in your case, but when i tried it my console >>>json_encode(['神州'], JSON_UNESCAPED_UNICODE); => "["神州"]" the proper output came
it shows nothing means the array itself might be empty
<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); $xx_1 = json_encode($xx, JSON_UNESCAPED_UNICODE); print_r($xx_1); ?>
0

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

$db_mysqli->set_charset('utf8');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.