【发布时间】:2017-08-11 22:47:13
【问题描述】:
我知道这个问题有很多问题,并且我已经阅读了大部分问题,当然包括“UTF-8 all way through”。
按照这些示例和提示,我将所有内容简化为这个最小示例 - 不幸的是,在对数组进行 json_encoding 之后仍然不会打印德语变音符号ö:
(这是问题 - 为什么?我还能做什么?)
<?php
error_reporting(E_ALL);
header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo "<br>ini_get('default_charset') ". ini_get('default_charset')."<br>"; // nothing shown
// if (!ini_set('default_charset', 'utf-8')) { // won't work (I guess I'm not allowed to do that)
// echo "could not set default_charset to utf-8<br>";
// }
echo "Köln"; // yay! displays "Köln" as expected
$darr = Array();
$locationString = mb_convert_encoding("location", "UTF-8");
$darr[$locationString] = mb_convert_encoding("Köln", "UTF-8");
$json = json_encode($darr);
echo $json;
// output:
// {"plain":"K\u00f6ln","utf_encode":"K\u00c3\u00b6ln","utf_decode":"K"}
// dah? why?
$array = json_decode($json);
var_dump($array);
// ... even worse: "Köln"
phpinfo();
?>
</body>
</html>
相关系统信息:
php 5.2.5(是的,我知道。我无法更改)
来自 phpinfo():
default_charset 无值
json
启用 json 支持
json 版本 1.2.1
mbstring
启用多字节支持
多字节字符串引擎 libmbfl
mbstring.encoding_translation 关 关
这可能是我的问题吗?
...是的,php 文件在 sublimeText 中编码为 utf-8(没有 BOM)。通过 FileZilla 提交给服务器一次作为 ASCII,一次二进制,没有变化。
【问题讨论】: