問題描述
我正在從外部服務器加載 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.
我該如何解決這個問題?
How can I solve this?
更新:
我嘗試將 HTML 保存到文件并使用 UTF-8 編碼輸出.兩者都不起作用,所以這意味著 file_get_contents() 已經(jīng)返回損壞的 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.
更新2:
<!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>
推薦答案
好的.我發(fā)現(xiàn) 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 字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!