file_get_contents()分解UTF-8字符

file_get_contents()分解UTF-8字符

我正在从外部服务器加载HTML。HTML标记具有UTF-8编码,并包含诸如ľ,š,č,ť,ž等字符。当我使用file_get_contents()加载HTML时,如下所示:


$html = file_get_contents('http://example.com/foreign.html');

它弄乱了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">

<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>


一只甜甜圈
浏览 1069回答 3
3回答

精慕HU

我和波兰语有类似的问题我试过了:$fileEndEnd&nbsp;=&nbsp;mb_convert_encoding($fileEndEnd,&nbsp;'UTF-8',&nbsp;mb_detect_encoding($fileEndEnd,&nbsp;'UTF-8',&nbsp;true));我试过了:$fileEndEnd&nbsp;=&nbsp;utf8_encode&nbsp;(&nbsp;$fileEndEnd&nbsp;);我试过了:$fileEndEnd&nbsp;=&nbsp;iconv(&nbsp;"UTF-8",&nbsp;"UTF-8",&nbsp;$fileEndEnd&nbsp;);然后 -$fileEndEnd&nbsp;=&nbsp;mb_convert_encoding($fileEndEnd,&nbsp;'HTML-ENTITIES',&nbsp;"UTF-8");这最后工作得很好!!!!!!

子衿沉夜

好的。我发现file_get_contents()没有导致这个问题。我在另一个问题中谈到了另一个不同的原因。傻我。
打开App,查看更多内容
随时随地看视频慕课网APP