73

我正在从外部服务器加载 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。

更新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>
4

11 回答 11

114

我对波兰语有类似的问题

我试过:

$fileEndEnd = mb_convert_encoding($fileEndEnd, 'UTF-8', mb_detect_encoding($fileEndEnd, 'UTF-8', true));

我试过:

$fileEndEnd = utf8_encode ( $fileEndEnd );

我试过:

$fileEndEnd = iconv( "UTF-8", "UTF-8", $fileEndEnd );

进而 -

$fileEndEnd = mb_convert_encoding($fileEndEnd, 'HTML-ENTITIES', "UTF-8");

最后一次完美!!!!!!!

于 2013-03-03T08:20:40.583 回答
86

file_get_contents 的 PHP 手册条目的注释中建议的解决方案

function file_get_contents_utf8($fn) {
     $content = file_get_contents($fn);
      return mb_convert_encoding($content, 'UTF-8',
          mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}

您也可以通过http://php.net/manual/en/function.mb-internal-encoding.php试试运气

于 2010-02-10T12:26:46.490 回答
6

好的。我发现 file_get_contents() 不会导致这个问题。我在另一个问题中谈到了一个不同的原因。傻我。

看到这个问题:为什么 DOM 改变编码?

于 2010-02-10T13:05:31.487 回答
5

我认为您只是在那里对字符类型进行了双重转换:D

可能是因为您在 html 文档中打开了 html 文档。所以你最终有一些看起来像这样的东西

<!DOCTYPE html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<!DOCTYPE html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>.......

因此,使用mb_detect_encoding可能会导致您遇到其他问题。

于 2012-11-10T18:59:00.633 回答
3

在土耳其语中,mb_convert_encoding 或任何其他字符集转换都不起作用。

由于空格字符转换为 + 字符,urlencode 也不起作用。对于百分比编码,它必须是 %20。

这个有效!

   $url = rawurlencode($url);
   $url = str_replace("%3A", ":", $url);
   $url = str_replace("%2F", "/", $url);

   $data = file_get_contents($url);
于 2016-10-26T08:24:31.963 回答
3

例子:

$string = file_get_contents(".../File.txt");
$string = mb_convert_encoding($string, 'UTF-8', "ISO-8859-1");
echo $string;
于 2019-09-17T09:26:00.343 回答
2

也试试这个

 $url = 'http://www.domain.com/';
    $html = file_get_contents($url);

    //Change encoding to UTF-8 from ISO-8859-1
    $html = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $html);
于 2014-11-19T13:55:28.563 回答
2

我设法使用下面的这个函数来解决:

function file_get_contents_utf8($url) {
    $content = file_get_contents($url);
    return mb_convert_encoding($content, "HTML-ENTITIES", "UTF-8");
}

file_get_contents_utf8($url);
于 2020-08-31T13:59:53.750 回答
0

我正在处理 35000 行数据。

$f=fopen("veri1.txt","r");
$i=0;
while(!feof($f)){
    $i++;
    $line=mb_convert_encoding(fgets($f), 'HTML-ENTITIES', "UTF-8");
    echo $line;
}

此代码将我的奇怪字符转换为正常字符。

于 2017-11-15T10:49:54.870 回答
0

我有一个类似的问题,解决它的是html_entity_decode

我的代码是:

$content = file_get_contents("http://example.com/fr");
$x = new SimpleXMLElement($content);
foreach($x->channel->item as $entry) {
    $subEntry = html_entity_decode($entry->description);
}

在这里,我正在检索一个 xml 文件(法语),这就是我使用这个 $x 对象变量的原因。然后我才把它解码成这个变量$subEntry

我试过mb_convert_encoding了,但这对我不起作用。

于 2019-10-18T01:42:25.710 回答
0

试试这个功能

function mb_html_entity_decode($string) {
if (extension_loaded('mbstring') === true)
{
    mb_language('Neutral');
    mb_internal_encoding('UTF-8');
    mb_detect_order(array('UTF-8', 'ISO-8859-15', 'ISO-8859-1', 'ASCII'));

    return mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
}

return html_entity_decode($string, ENT_COMPAT, 'UTF-8');

}

于 2020-01-22T19:55:34.643 回答