简体   繁体   中英

json_encode not working uft8 content php

I have to show json data from my local database and this is Bangla font. It is working when normal print this data but when use json_encode then not show proper data, table field set uft8.

Here is my code which I have tried:

$mysqli = new mysqli("localhost", "root", "", "newspaper");
mysqli_set_charset($mysqli,"utf8");
$stmt = $mysqli->prepare("SELECT news_title FROM news_content ORDER BY id DESC limit 2");
$stmt->execute();
$result = $stmt->get_result();

while ($myrow = $result->fetch_assoc()) {
    $data[] =  $myrow['news_title'];
}
print_r($data);
echo json_encode($data);

this is Output when print_r($data); :

Array
(
    [0] => বুলবুলির ছানা
    [1] => টনি ডায়েসের প্রিয় সামিনা চৌধুরী, মেয়ের সেলেনা গোমেজ
)

and:

 echo json_encode($data); 

Output is:

["\u09ac\u09c1\u09b2\u09ac\u09c1\u09b2\u09bf\u09b0 \u099b\u09be\u09a8\u09be","\u099f\u09a8\u09bf \u09a1\u09be\u09df\u09c7\u09b8\u09c7\u09b0 \u09aa\u09cd\u09b0\u09bf\u09df \u09b8\u09be\u09ae\u09bf\u09a8\u09be \u099a\u09cc\u09a7\u09c1\u09b0\u09c0, \u09ae\u09c7\u09df\u09c7\u09b0 \u09b8\u09c7\u09b2\u09c7\u09a8\u09be \u0997\u09cb\u09ae\u09c7\u099c"] 

I have also tried:

echo json_encode($data, JSON_UNESCAPED_UNICODE);

and:

header('Content-Type: application/json');
echo json_encode($data, JSON_UNESCAPED_UNICODE);

I am not sure which problem is, so any help it will be very helpful for me.

The right way is to use json_encode with JSON_UNESCAPED_UNICODE flag. Then Unicode characters won't be escaped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM