file_get_contents()分解UTF-8字符 [英] file_get_contents() Breaks Up UTF-8 Characters
问题描述
我正在从外部服务器加载HTML. HTML标记具有UTF-8编码,并包含ľ,š,č,ť,ž等字符.当我使用file_get_contents()加载HTML时,如下所示:
I am loading a HTML from an external server. The HTML markup has UTF-8 encoding and contains characters such as ľ,š,č,ť,ž etc. When I load the HTML with file_get_contents() like this:
$html = file_get_contents('http://example.com/foreign.html');
它弄乱了UTF-8字符并加载了Å,¾,¤和类似的废话,而不是正确的UTF-8字符.
It messes up the UTF-8 characters and loads Å, ¾, ¤ and similar nonsense instead of proper UTF-8 characters.
我该如何解决?
更新:
我尝试将HTML保存到文件中并以UTF-8编码输出.两者都不起作用,所以这意味着file_get_contents()已经返回损坏的HTML.
I tried both saving the HTML to a file and outputting it with UTF-8 encoding. Both doesn't work so it means file_get_contents() is already returning broken 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>
推荐答案
好的.我发现file_get_contents()不会导致此问题.我在另一个问题中谈到的原因是不同的.愚蠢的我.
Alright. I have found out the file_get_contents() is not causing this problem. There's a different reason which I talk about in another question. Silly me.
请参阅以下问题: DOM为什么要更改编码?
这篇关于file_get_contents()分解UTF-8字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!