【发布时间】:2016-07-09 06:24:53
【问题描述】:
我尝试从我的服务器获取用希腊语编写的数据。它们存储得很好,但是当我阅读它们时,我遇到了如图所示的问题。
[{"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');
【问题讨论】:
-
你的整个链 UTF-8 干净吗?如果沿途有诸如 Latin1 或 Windows-1252 之类的东西,您可能会遇到编码问题。连接、数据库、列类型都需要匹配。
-
是的,它们很干净。我的字段日期和游戏我有 uf8_general_ci。
-
我有 php 版本 7
-
检查表的编码。做一个
show create table last_game。 -
你确定这是 PHP7 吗?
JSON_UNESCAPED_UNICODE建议 PHP 5.3.x 或更低版本。