【发布时间】:2013-10-22 01:36:13
【问题描述】:
我一直在获取此函数检索到的名称的 UTF8 版本,但由于某种原因,它没有以正确的字母输出它。
示例输出:
ѕqÃ…ιÑÂтℓє
预期输出:
ѕqυιятℓє
我已经对文件、从 file_get_contents 中提取的字符串和函数的输出以及源 XML 文件进行了字符集检查。 MySQL 也没有得到正确的版本。
此外,SimpleXML 确实支持 UTF-8。
sudo 文件 -i debug.txt
debug.txt: txt/plain; charset=utf-8
MySQL 排序规则
utf8_general_ci
源 XML 文件头:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
这个(在代码中出现了两次)-
$enc = mb_detect_encoding($xml, 'UTF-8', true);
echo 'Detected encoding '.$enc;
输出:
Detected encoding UTF-8
Detected encoding UTF-8
我不确定在哪里检查 UTF-8 字符集,或者这是否是我首先需要做的。我希望这里有人知道如何获得名称的预期版本。提前谢谢。
TextFile(日志)输出函数:
function log_output($message){
$file = 'debug.txt';
$current = file_get_contents($file);
$current .= $message."\n";
file_put_contents($file, $current);
}
源代码:
// Converts SteamID(64) to the users current name on Steam.
function steamid64_to_name($steamid64) {
// Get User Profile Data
$xml = file_get_contents('http://steamcommunity.com/profiles/'.$steamid64.'/?xml=1');
$enc = mb_detect_encoding($xml, 'UTF-8', true);
echo 'Detected encoding '.$enc;
$xml = simplexml_load_string($xml, null, LIBXML_NOCDATA);
if(!empty($xml)) {
if(isset($xml->steamID)) {
$username = $xml->steamID;// Example: steamcommunity.com/profiles/76561198077095013/?xml=1
} else {
$username = "Username Not Found";
}
} else {
$username = "User XML Not Found"; // Example: steamcommunity.com/profiles/0/?xml=1
}
$enc = mb_detect_encoding($xml, 'UTF-8', true);
echo 'Detected encoding '.$enc;
return $username;
}
【问题讨论】:
-
这个用户名怎么输出?
-
更新了 OP 以显示 MySQL Collation 和将其输出到允许我读取它的文件的函数。
-
您是否使用知道它是 UTF-8 文件的编辑器查看日志文件?
-
我正在通过具有 UTF-8 功能的浏览器 (Chrome) 查看文件,还通过 Sublime Text 使用 FTP,并且 MySQL(通过 Chrome)显示相同的名称。
-
@Caffeine 可以通过
header("Content-Type: text/html; charset=utf8");指定字符集