我试图使用UTF-8并遇到麻烦.
我尝试了很多东西; 这是我得到的结果:
????而不是亚洲人物.即使是欧洲文本,我也得到Se?or了Señor.Señor或新浪新闻为????.Sefor Señor.我究竟做错了什么?我该如何修复代码?我可以恢复数据,如果是,如何恢复?
我正在从外部服务器加载HTML.HTML标记具有UTF-8编码,并包含诸如ľ,š,č,ť,ž等字符.当我使用file_get_contents()加载HTML时,如下所示:
$html = file_get_contents('http://example.com/foreign.html');
Run Code Online (Sandbox Code Playgroud)
它弄乱了UTF-8字符并加载Å,¾,¤和类似的废话而不是正确的UTF-8字符.
我怎么解决这个问题?
更新:
我尝试将HTML保存到文件并使用UTF-8编码输出.两者都不起作用,这意味着file_get_contents()已经返回损坏的HTML.
UPDATE2:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sk" lang="sk">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Language" content="sk" />
<title>Test</title>
</head>
<body>
<?php
$html = file_get_contents('http://example.com');
echo htmlentities($html);
?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 这是简单的代码
<?php
$var = "?????? ????? ?.?. ????? ?????????? ??? ???? ???? ? ?????? ? ?? ???????? ????????? ? ??? ???????????? ?????????? ????? ????????? ????? ? ???????, ??????, ?????? ? ??????. ? ???????? ???? 3 885 ??????? ???????????? ????????????, ??????? ????????? ??? (???) ??????? ??? ????????? ?????? ????????, ?????????????? 5 000 ???????, ???? ????????? ???????????? ?????????, 12 ??????????, ? 26 ?????? ???????? ?????????.";
$foo = substr($var,0,142);
echo $foo;
?>
Run Code Online (Sandbox Code Playgroud)
并输出如下内容:
БензинОфисиА.С.такжепроизводитвсетипыжираисмазокиихпобочныхпродук...
我试过没有运气的mb_substr().如何以正确的方式做到这一点?
我没有看到任何违法行为 - 对可能出现的问题提出任何建议?
if (strtolower($matches[1]) != 'utf-8') {
var_dump($matches[1]);
$xml = iconv($matches[1], 'utf-8', $xml);
$xml = str_replace('encoding="'.$matches[1].'"', 'encoding="utf-8"', $xml);
}
Run Code Online (Sandbox Code Playgroud)
下面是我的调试/错误
string(12) "windows-1252"
Notice (8): iconv() [http://php.net/function.iconv]: Detected an illegal character in input string [APP/models/sob_form.php, line 16]
Run Code Online (Sandbox Code Playgroud)
我已经验证上面的代码确实是第16行