【问题标题】:Greeks letters and JSON response希腊字母和 JSON 响应
【发布时间】: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 或更低版本。

标签: php mysql json


【解决方案1】:

在访问数据库之前,最好是在连接建立之后,执行以下操作:

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,您需要将其更改为utf8utf8_general_ciutf8_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;

【讨论】:

  • 对不起,我应该把 SHOW CREATE TABLE last_game 放在哪里以及如何放置;
  • 我使用在线服务器
  • 如果不存在则创建表 last_game (id int(10) NOT NULL AUTO_INCREMENT, date text NOT NULL, game text NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB 默认字符集=utf8_general_ci AUTO_INCREMENT=30;
  • 好的。我解决了这个问题。你知道问题是什么吗?我使用的是 php v 5.3.3 :)。我改成了 5.6 的东西,现在可以工作了。:)。非常感谢您的回答。我学到了一些新东西:)。
  • 是的,使用更现代的 PHP 和 JSON_UNESCAPED_UNICODE 绝对是最好的方法 :) PHP 中的 Unicode 和 UTF-8 非常烦人,尤其是在旧版本中 :) 没问题,很高兴为您提供帮助!
猜你喜欢
  • 2021-07-16
  • 2021-07-02
  • 2014-12-21
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 2013-01-28
相关资源
最近更新 更多