1

I have a non-english word e.g 'परीक्षण' in database.I fetch the data from slim framework (Php) and the code is :

        while($row=$result->fetch_assoc()){
         $data[]=$row;
        }
         return $response->withStatus(200)
        ->withHeader('Content-Type', 'application/json')
        ->write(json_encode($data));

The response is for e.g;

          [{"s.n":"1","english_word":"test","nepali_word":"???????"}]

Could someone please tell me how can I send the non-english word in Json from php?

Ramesh Khadka
  • 484
  • 1
  • 7
  • 19

3 Answers3

4

you may want to use the JSON_UNESCAPED_UNICODE constant

$arr = ['s.n' => 1, 'english_word' => 'test', 'nepali_word' => 'परीक्षण'];

echo json_encode($arr, JSON_UNESCAPED_UNICODE);

// Output : {"s.n":1,"english_word":"test","nepali_word":"परीक्षण"}

live demo : https://3v4l.org/DVejS


in your issue context , when using headers like Content-Type: application/json you will need to set the charset too as follows:

->withHeader('Content-Type', 'application/json;charset=utf-8;')
hassan
  • 7,812
  • 2
  • 25
  • 36
0

Use the following code:

while($row=$result->fetch_assoc()){
        $data[]=$row;
}
return $response->withStatus(200)
    ->withHeader('Content-Type', 'application/json')
    ->write(json_encode(mb_convert_encoding($data, "HTML-ENTITIES", "UTF-8")));
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
umair shah
  • 39
  • 3
  • yes you can also tried this. $data = "यह कार है"; echo json_encode(mb_convert_encoding($data, "HTML-ENTITIES", "UTF-8")); – umair shah Mar 28 '17 at 12:06
  • first error you will get from your code is that mb_convert_encode only accepts a string while you are sending an array to it. `$data` is an array :) – hassan Mar 28 '17 at 12:08
-1

there are couple of possibilities in these situations:

possibility -1: your database column encoding might be not supporting the characters (check the encoding of the column and use utf8mb4 for mysql). in that case when you insert not supported character like "ع" , the database will convert it to real actual question mark "?" . you lost the characters forever and you will still see question marks ??? whatever you do

possibility -2: your database is using the correct encoding and you have your characters stored in the database correctly, but you still seeing question marks "?????" . in that case the program that showing the characters (text editor,web browser,...etc ) is using a wrong encoding.

what to do now ?

1- check your database column encoding to see if it is supports utf-8.()

2- check the encoding of the whatever program you are using the characters throw (browser, text editor,Mysql workbench,..etc )

Accountant م
  • 6,975
  • 3
  • 41
  • 61