我尝试从我的服务器获取用希腊语编写的数据。它们存储得很好,但是当我读它们时,我遇到了一个问题。
[{"id":"22","game":"??? 3 - 0 ?????","date":"27th August, 2016"}]
我正在添加
JSON_UNESCAPED_UNICODE
在我的json_encode()
函数中但知道我明白了。
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
这是我的PHP代码,它读取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;
任何想法如何解决?
感谢。
西奥。
修改
我这样做但是我收到了错误。
<?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);
?>
在这一行:
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
答案 0 :(得分:2)
在访问数据库之前,最好在建立连接后立即执行以下操作:
mysqli_query($con, "SET CHARACTER SET utf8");
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET SESSION collation_connection = 'utf8_general_ci'");
还要确保您的表是使用UTF-8编码为文本字段创建的。要检查这一点,请使用:
SHOW CREATE TABLE last_game;
如果您看到latin1
,我们想要将其更改为utf8
,utf8_general_ci
或utf8_unicode_ci
。
在输入和存储数据时,还要确保数据为UTF-8。
编辑:您似乎正在从JSON解析中获取转义的unicode。可能是因为你有一个旧版本的PHP。你可以这样做来转换它:
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 >
有关如何在较旧的PHP版本中模拟JSON_UNESCAPED_UNICODE
的其他示例,请参阅here。
修改:将修改后的代码更改为:
<?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;