相关疑难解决方法(0)

UTF-8字符有问题; 我看到的不是我存储的东西

我试图使用UTF-8并遇到麻烦.

我尝试了很多东西; 这是我得到的结果:

  • ????而不是亚洲人物.即使是欧洲文本,我也得到Se?orSeñor.
  • 奇怪的胡言乱语(Mojibake?)如Señor新浪新闻????.
  • 黑色钻石,如Se or.
  • 最后,我陷入了数据丢失或至少被截断的情况:Sefor Señor.
  • 即使我看到正确的文字,它也没有正确排序.

我究竟做错了什么?我该如何修复代码?我可以恢复数据,如果是,如何恢复?

mysql unicode utf-8 character-encoding mariadb

66
推荐指数
3
解决办法
3万
查看次数

file_get_contents()分解UTF-8字符

我正在从外部服务器加载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 utf-8 file-get-contents

58
推荐指数
5
解决办法
10万
查看次数

带有utf-8的php substr()函数在最后留下 标记

这是简单的代码

<?php

$var = "?????? ????? ?.?. ????? ?????????? ??? ???? ???? ? ?????? ? ?? ????????        ????????? ? ??? ???????????? ?????????? ????? ????????? ????? ? ???????, ??????, ?????? ? ??????. ? ???????? ???? 3 885 ??????? ???????????? ????????????, ??????? ????????? ??? (???) ??????? ??? ????????? ?????? ????????, ?????????????? 5 000 ???????, ???? ????????? ???????????? ?????????, 12 ??????????, ? 26 ?????? ???????? ?????????.";

$foo = substr($var,0,142);

echo $foo;
?>
Run Code Online (Sandbox Code Playgroud)

并输出如下内容:

БензинОфисиА.С.такжепроизводитвсетипыжираисмазокиихпобочныхпродук...

我试过没有运气的mb_substr().如何以正确的方式做到这一点?

php utf-8 substr

54
推荐指数
3
解决办法
5万
查看次数

iconv - 检测到输入字符串中的非法字符

我没有看到任何违法行为 - 对可能出现的问题提出任何建议?

    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行

php iconv

29
推荐指数
4
解决办法
9万
查看次数