Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php file_get_contents()将UTF-8转换为ISO-8859-1_Php_Utf 8_File Get Contents_Iso 8859 1 - Fatal编程技术网

Php file_get_contents()将UTF-8转换为ISO-8859-1

Php file_get_contents()将UTF-8转换为ISO-8859-1,php,utf-8,file-get-contents,iso-8859-1,Php,Utf 8,File Get Contents,Iso 8859 1,我正在尝试从中获取搜索结果 但是file_get_contents()将UTF-8字符集(yahoo使用的字符集)内容转换为ISO-8859-1 尝试: 脚本作为 header('Content-Type: text/html; charset=UTF-8'); 或 或 或 没有帮助,因为在获得web内容后,所有特殊字符(如ťžžžžžžžžžžž)都会被 我将感谢任何帮助 $s2 = iconv("ISO-8859-1","UTF-8//TRANSLIT//IGNORE",$filename

我正在尝试从中获取搜索结果

但是file_get_contents()将UTF-8字符集(yahoo使用的字符集)内容转换为ISO-8859-1

尝试:

脚本作为

header('Content-Type: text/html; charset=UTF-8');

没有帮助,因为在获得web内容后,所有特殊字符(如ťžžžžžžžžžžž)都会被

我将感谢任何帮助

$s2 = iconv("ISO-8859-1","UTF-8//TRANSLIT//IGNORE",$filename );
更好的解决方案

function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_ENCODING, 1);
    return curl_exec($ch);
    curl_close($ch);
}

echo curl($filename);
文件\u get\u内容不应更改字符集。数据作为二进制字符串拉入

签出您提供的url时,这是它提供的标题:

Content-Type: text/html; charset=ISO-8859-1
此外,在体内:

<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

此外,您不能将UTF-8无损转换为ISO-8859-1,并在返回UTF-8时返回字符。UTF-8/unicode支持更多的字符,因此字符在第一步就丢失了

在浏览器中,情况并非如此,因此您可能只需要提供一个正确的Accept Encoding标头,以指示yahoo的系统可以接受UTF-8。

这似乎是一个问题,因为
文件获取内容
可能发送一个只接受ISO 8859-1作为字符编码的请求

您可以创建一个自定义的
文件\u get\u内容
,使用该文件明确声明您接受UTF-8:

$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);

$filename = "http://search.yahoo.com/search;_ylt=A0oG7lpgGp9NTSYAiQBXNyoA?p=naj%C5%A1%C5%A5astnej%C5%A1%C3%AD&fr2=sb-top&fr=yfp-t-701&type_param=&rd=pref";
echo file_get_contents($filename, false, $context);

任何对此进行调查的人:

我花在编码问题上的时间告诉我,很少有php函数“神奇地”改变字符串的编码。(其中一个罕见的例子是:exec($command,$output,$returnVal)

exec( $command, $output, $returnVal ) 还请注意,工作标题集如下所示:

header('Content-Type: text/html; charset=utf-8'); 标题('Content-Type:text/html;charset=utf-8'); 而不是:

header('Content-Type: text/html; charset=UTF-8'); 标题('Content-Type:text/html;charset=UTF-8'); 因为我有一个与您描述的问题类似的问题,所以正确设置标题就足够了


希望这有帮助!

结果是:文档已移到此处。@vladinko0,我想您需要设置
CURLOPT\u FOLLOWLOCATION
,我已更新了答案,请重试。现在它加载页面,但结果与file\u get\u contents()相同它的意思是带有问号。字符集也被转换为ISO-8859-1。看起来yahoo.com根据您的IP(国家)提供不同的页面(字符集).我已将您的URL更改为
http://ru.search.yahoo.com
但它不起作用。也许你可以通过接受字符集标题、拒绝ISO-8859-1实现一些功能……你是如何发现
内容类型:text/html;charset=ISO-8859-1
当我查看该页面的源代码时,我看到
它提供不同的编码的根据您的位置,您可以尝试使用俄罗斯代理服务器获取页面。文件\u get\u contents()不会转换任何有趣的东西,我尝试了
Accept Charset=utf-8;q=0.7,*;q=0.7
,但不起作用:)@webarto:The value
utf-8;q=0.7,*;q=0.7
类似于
utf-8,*
并且可以平等地接受任何字符编码。不错的秋葵!我在url(Munchen)中努力使用umlauts,这解决了问题。谢谢
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);

$filename = "http://search.yahoo.com/search;_ylt=A0oG7lpgGp9NTSYAiQBXNyoA?p=naj%C5%A1%C5%A5astnej%C5%A1%C3%AD&fr2=sb-top&fr=yfp-t-701&type_param=&rd=pref";
echo file_get_contents($filename, false, $context);
exec( $command, $output, $returnVal ) header('Content-Type: text/html; charset=utf-8'); header('Content-Type: text/html; charset=UTF-8');