繁体   English   中英

file_get_contents() 分解 UTF-8 字符

[英]file_get_contents() Breaks Up UTF-8 Characters

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

我对波兰语有类似的问题

我试过:

$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");

最后一次完美地工作!!!!!!!

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

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

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

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

我认为您只是在那里对字符类型进行了双重转换: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可能会导致您遇到其他问题。

土耳其语、mb_convert_encoding 或任何其他字符集转换均无效。

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

这个成功了!

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

   $data = file_get_contents($url);

例子:

$string = file_get_contents(".../File.txt");
$string = mb_convert_encoding($string, 'UTF-8', "ISO-8859-1");
echo $string;

也试试这个

 $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);

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

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

我正在处理 35000 行数据。

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

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

我有一个类似的问题,解决它的是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但这对我不起作用。

试试这个功能

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');

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM