UTF-8 字符编码与 json_encode() 的争论

14 浏览
0 Comments

UTF-8 字符编码与 json_encode() 的争论

这个问题已有答案:

UTF-8全程支持

问题

我想获取包含重音字符的行。列 (NAME) 的编码为 latin1_swedish_ci

代码

下面的查询在 phpMyAdmin 中返回 Abord â Plouffe

SELECT C.NAME FROM CITY C
WHERE C.REGION_ID=10 AND C.NAME_LOWERCASE LIKE '%abor%'
ORDER BY C.NAME LIMIT 30

下面的函数调用 (db_fetch_all( $result )) 返回期望的值:

  while( $row = mysql_fetch_assoc( $result ) ) {
    foreach( $row as $value ) {
      echo $value . " ";
      $value = utf8_encode( $value );
      echo $value . " ";
    }
    $r[] = $row;
  }

显示的值为:5482 5482 Abord â Plouffe Abord â Plouffe

然后使用 json_encode 对数组进行编码:

$rows = db_fetch_all( $result );
echo json_encode( $rows );

问题

Web 浏览器接收到了以下值:

{"ID":"5482","NAME":null}

而不是:

{"ID":"5482","NAME":"Abord â Plouffe"}

(或编码等效的值。)

问题

文档说明 json_encode() 适用于 UTF-8。我能看到值从 LATIN1 编码成 UTF-8。然而,在调用 json_encode() 后,值变成了 null

如何使 json_encode() 正确地编码 UTF-8 值?

一种可能的解决方案是使用 Zend 框架,但如果可以避免的话,我宁愿不用。

admin 更改状态以发布 2023年5月24日
0
0 Comments

foreach( $row as $value ) {
  $value = utf8_encode( $value );

你实际上没有将编码值写回到$row数组中,你只改变了本地变量$value。如果你想在更改变量时写回,你需要将它视为引用:

foreach( $row as &$value ) {

个人建议尽可能避免使用引用,在这种情况下,可以尝试使用Kemo发布的array_map

或者使用mysql_set_charset转换为UTF-8,无论实际表排序如何,都可以获得以UTF-8返回值,作为迁移到UTF-8应用的第一步。

0
0 Comments
// Create an empty array for the encoded resultset
$rows = array();
// Loop over the db resultset and put encoded values into $rows
while($row = mysql_fetch_assoc($result)) {
  $rows[] = array_map('utf8_encode', $row);
}
// Output $rows
echo json_encode($rows);

(这是一段包含粗体标记的文本)

0