1

I try get data from my server which are written in Greek. They are stored fine,but when I read them I have a problem as shown.

[{"id":"22","game":"??? 3 - 0 ?????","date":"27th August, 2016"}]

I am adding the

JSON_UNESCAPED_UNICODE

inside my json_encode() function but know I get this.

 Notice: Use of undefined constant JSON_UNESCAPED_UNICODE - assumed 'JSON_UNESCAPED_UNICODE' in /var/www/vhosts/theo-android.co.uk/httpdocs/ael/android/last_game_json.php on line 25

 Warning: json_encode() expects parameter 2 to be long, string given in /var/www/vhosts/theo-android.co.uk/httpdocs/ael/android/last_game_json.php on line 25

This is my PHP code that reads the JSON.

<?php 
include("../init.php");

$string="";
$newString="";
$get_posts = "select * from last_game";

error_reporting(E_ALL); 
ini_set("display_errors", 1);

$run_posts = mysqli_query($con, $get_posts); 

$posts_array = array();

while ($posts_row = mysqli_fetch_array($run_posts)) {
    $row_array['id'] = $posts_row['id'];
    $row_array['game'] = $posts_row['game'];
    $row_array['date'] = $posts_row['date'];

    array_push($posts_array,$row_array);            
 }

 $string = json_encode($posts_array, JSON_UNESCAPED_UNICODE);      
 echo $string;

Any ideas how to fix it?

Thanks.

Theo.

EDIT

I did this but I get an error.

<?php 
include("init.php");

$get_posts = "select * from last_game";
error_reporting(E_ALL); 
ini_set("display_errors", 1);
$run_posts = mysqli_query($con,$get_posts); 

 $posts_array = array();

 while ($posts_row = mysqli_fetch_array($run_posts)){


               $row_array['id'] = $posts_row['id'];
               $row_array['game'] =$posts_row['game'];
               $row_array['date'] = $posts_row['date'];


               array_push($posts_array,$row_array);

 }
    $str = '\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7';
    $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
  (return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
   ( }, $str);
  //$string = json_encode(utf8_encode($posts_array));
  //$response = utf8_encode($string,true);
  //echo $response;
   print($str);

?>

in this line:

return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
Theo
  • 3,099
  • 12
  • 53
  • 94

1 Answers1

2

Before you access your database, preferably right after the connection is set up, do this:

mysqli_query($con, "SET CHARACTER SET utf8");
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET SESSION collation_connection = 'utf8_general_ci'");

Also ensure that your table was created using UTF-8 encoding for text fields. To check this, use:

SHOW CREATE TABLE last_game;

If you see latin1 you'll want to change this to utf8, and utf8_general_ci or utf8_unicode_ci.

Also ensure that the data is in UTF-8 when it is input and stored.

Edit: It looks like you're getting escaped unicode from the JSON parsing. Probably because you have an older version of PHP. You can do this to convert it:

php > $str = '\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7';
php > $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
php (     return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
php ( }, $str);
php >
php > print($str);
ΑΕΛ 3 - 0 Ξανθη
php >

See here for other examples of how to emulate JSON_UNESCAPED_UNICODE in older PHP versions.

Edit: Change your edited code to this:

<?php 
include("../init.php");

// Probably just put this in init.php.
mysqli_query($con, "SET CHARACTER SET utf8");
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET SESSION collation_connection = 'utf8_general_ci'");

$string="";
$newString="";
$get_posts = "select * from last_game";

error_reporting(E_ALL); 
ini_set("display_errors", 1);

$run_posts = mysqli_query($con, $get_posts); 

$posts_array = array();

while ($posts_row = mysqli_fetch_array($run_posts)) {
    $row_array['id'] = $posts_row['id'];
    $row_array['game'] = $posts_row['game'];
    $row_array['date'] = $posts_row['date'];

    array_push($posts_array, $row_array);            
}

$string = json_encode($posts_array);
$string = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $string);

echo $string;
Will
  • 24,082
  • 14
  • 97
  • 108
  • Sorry where and how should I put SHOW CREATE TABLE last_game; – Theo Jul 09 '16 at 08:09
  • I use an online server – Theo Jul 09 '16 at 08:12
  • CREATE TABLE IF NOT EXISTS `last_game` ( `id` int(10) NOT NULL AUTO_INCREMENT, `date` text NOT NULL, `game` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8_general_ci AUTO_INCREMENT=30; – Theo Jul 09 '16 at 08:19
  • This is how I created my table. – Theo Jul 09 '16 at 08:20
  • I get this response now. – Theo Jul 09 '16 at 08:42
  • [{"id":"30","game":"\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7","date":"27th August, 2016"}] – Theo Jul 09 '16 at 08:42
  • If you echo `"\u0391\u0395\u039b 3 - 0 \u039e\u03b1\u03bd\u03b8\u03b7"` you should get the literal UTF-8 text. – Will Jul 09 '16 at 08:44
  • So what should I do next? – Theo Jul 09 '16 at 08:46
  • 1
    Ok. I fixed the problem. Do you know what the problem was? I was using php v 5.3.3:). I changed to 5.6 something and works now.:). Many thanks for your answer. I learned some new things:). – Theo Jul 09 '16 at 09:07
  • 1
    Yeah, using more modern PHP and `JSON_UNESCAPED_UNICODE` is definitely the best way :) Unicode and UTF-8 in PHP are quite annoying, especially in older versions :) No problem, glad to help! – Will Jul 09 '16 at 09:09