gpt4 book ai didi

php - 来自 MariaDB、Drupal、php 的 UTF-8 字符

转载 作者:行者123 更新时间:2023-11-29 16:38:11 25 4
gpt4 key购买 nike

所以我有 MARIADB 集群数据库,其中我的字符集设置为:utf8_general_ci

我还在 Drupal 模块内部提供了 PHP 函数,用于 DB_Selects 行并最终将它们返回为“JSON”。

问题是它返回带有编码的 UTF-8 字符,但它们应该被解码..

示例:Ü 返回为:\u00dc

php 函数我有

function jgid_response($field, $id) {
drupal_add_http_header("Expires", -1);
drupal_page_is_cacheable(FALSE);

$res = db_select('TABLE1', 'ur')
->fields('ur', array('Cust_id', 'User_id'));

$res->addExpression('IF(begin > 0, DATE_FORMAT(FROM_UNIXTIME(begin), \'%d.%m.%Y\'), 0)', 'begin');
$res->addExpression('IF(end > 0, DATE_FORMAT(FROM_UNIXTIME(end), \'%d.%m.%Y\'), 0)', 'end');
$res->join('TABLE2', 'PERSON', 'person.id = ur.Cust_id');
$res->join('TABLE2', 'COMPANY', 'company.id = ur.User_id');

$res->fields('person', array('firstName', 'lastName'));
$res->fields('company', array('name'));

$res->condition($field, $id)
->condition('ur.notActive', 0);

$string = (string) $res;

$res = $res->execute();

if ($res->rowCount() > 0) {
$data = array();

while ($result = $res->fetchObject()) {

$result->application = 'APPLICATION_NAME';

$data[] = $result;
}

return array(
'result' => 'OK',
'list' => $data,
);
}
}

有什么办法可以强制该数组编码字符吗?我已经尝试过:JSON_PRETTY_PRINT、JSON_UNESCAPED_SLASHES 以及 JSON_UNESCAPED_UNICODE

还尝试添加:

drupal_add_http_header("X-JSON-RESPONSE", 1);

drupal_add_http_header('Content-Type', 'text/csv; utf-8');

我哪里错了?

最佳答案

带有转义 Unicode 字符的 JSON 有效。您没有向我们展示您的 php 程序在哪里序列化 JSON。内置php json_encode()方法以这种方式生成它。

JSON.parse()您的浏览器应该会以正确的 unicode 表示方式返回数据。

请注意,您无法通过使用 Content-Type 或其他 header 来影响 JSON 的编码方式。为什么不? JSON 本身决定了其内部格式。

由于其历史来自 Unicode 出现之前,php 通常尽可能使用 /u 样式编码。

关于php - 来自 MariaDB、Drupal、php 的 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com