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');