我尝试从我的服务器获取数据,这些数据是用希腊语编写的。它们存储得很好,但是当我读到它们时,我遇到了一个问题,如下所示。
[{"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这是我读取JSON的PHP代码。
<?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');发布于 2016-07-09 07:58:31
在访问数据库之前,最好是在建立连接之后,执行以下操作:
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 >有关如何在旧版本中模拟here的其他示例,请参阅JSON_UNESCAPED_UNICODE。
编辑:将编辑的代码更改为:
<?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;https://stackoverflow.com/questions/38279035
复制相似问题