file_get_contents show characters of utf-8 like question marks

喜夏-厌秋 提交于 2019-12-23 02:24:28

问题


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

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.

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.

I would appreciate any help very.

UPDATE:

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


回答1:


[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]

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




回答2:


try once this code it worked for me ..

    <?php 
$abc = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$some_context = stream_context_create($abc);
$filename = "https://www.google.com/search?q=sd";
echo file_get_contents($filename, false, $some_context);
    ?>


来源:https://stackoverflow.com/questions/39290627/file-get-contents-show-characters-of-utf-8-like-question-marks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!