我正在从外部服务器加载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" 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
因此,使用可能会导致您遇到其他问题。
也尝试一下
$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);
土耳其语,mb_convert_encoding或任何其他字符集转换均无效。
而且urlencode也不起作用,因为将空格char转换为+ char。百分比编码必须为%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;
我设法解决以下使用此功能:
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');
}
文章标签:file-get-contents , php , utf-8
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!