file_get_contents显示utf-8字符,例如问号 [英] file_get_contents show characters of utf-8 like question marks

查看:182
本文介绍了file_get_contents显示utf-8字符,例如问号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$str = "https://www.google.com/search?q=sd";
echo file_get_contents($str);

我正在尝试使用功能file_get_contents从google加载页面,但是utf-8的chracters的输出显示为问号.

I'm try to load page from google with the function file_get_contents, but the output of chracters of utf-8 showing like question mark.

我尝试了这里介绍的所有可能性: file_get_contents()将UTF-8转换为ISO-8859- 1 但是问题仍然没有解决.

I've tried all the possibilities introduced here: file_get_contents() converts UTF-8 to ISO-8859-1 But the problem is still not resolved.

非常感谢您的帮助.

更新:

我发现问题是通过Google存在的,其他网站的内容显示正确.

I see that the problem exists through Google, other sites content is displayed correctly.

推荐答案

[php]

//charset.php?case=1
//charset.php?case=2
//charset.php?case=3

$case = isset($_GET['case']) ? $_GET['case'] : 1;

if( !in_array($case,range(1,3)) ) $case = 1;


if( $case==1 ) {
    header("Content-type: text/html; charset=tis-620"); //http://htmlpurifier.org/docs/enduser-utf8.html
    $str = "https://www.google.co.th/search?q=sd";
}

if( $case==2 ) {
    header("Content-type: text/html; charset=ISO-8859-1");
    $str = "https://www.google.de/search?q=sd";
}   

if( $case==3 ) {
    header("Content-type: text/html; charset=ISO-8859-9");
    $str = "https://www.google.com.tr/search?q=sd";
}


$data = file_get_contents($str);
echo $data;

[/php]

如您所见... php标头中正确的字符集就是解决方案

as you can see ... the correct charset in php header is the solution

这篇关于file_get_contents显示utf-8字符,例如问号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆