我在php页面中通过file_get_contents获取百度的内容,我的php页面编码是utf-8的,百度的gb2312,我用inconv转码之后输出仍是乱码,是什么原因呢?代码如下:
$url = "http://www.baidu.com/s?f=8&wd=煤层";
$html = file_get_contents($url);
$html = iconv("gb2312", "utf-8//IGNORE",$html);
print_r ($html);

解决方案 »

  1.   

    本帖最后由 xuzuning 于 2010-07-15 15:22:19 编辑
      

  2.   

    <?php
    header("Content-Type:text/html;charset=utf-8");
    $keyworld="煤层";
    $keyworld=iconv("utf-8","gb2312",$keyworld);
    $url = "http://www.baidu.com/s?f=8&wd=$keyworld";
    $html = file_get_contents($url);
    $html = iconv("gb2312", "utf-8//IGNORE",$html);
    echo $html;
    ?> 
      

  3.   

     $url = "http://www.baidu.com/s?f=8&wd=".iconv('utf-8','gbk','煤层');
    $html = file_get_contents($url);
    $html = iconv("gbk", "utf-8//IGNORE",$html);      
    print_r ($html);
      

  4.   

     
    $url = "http://www.baidu.com/s?f=8&wd=煤层";
    $html = file_get_contents($url);
    gb2312ToUtf8($html);//可判断是字符串或者数组,自动进行转换;
    print_r ($html);        function gb2312ToUtf8(&$input)
            {
                    if (!is_array($input))
                    {
                            $input = iconv('GB2312', 'UTF-8', $input);
                    }
                    else
                    {
                            foreach ($input as $k=>$v)
                            {
                                    self::gb2312ToUtf8(&$input["$k"]);
                            }
                    }
            }
      

  5.   

     self::  这个去掉 
      

  6.   

    把body中的内容提取出来 头部不要了不就OK!
      

  7.   

    用了3楼的方法地可以了,但是中间有空格的时候还是有问题:header("Content-Type:text/html;charset=utf-8");
    $keyworld="环境样品 水中氟化物含量";
    $keyworld=iconv("utf-8","gb2312",$keyworld);
    $url = "http://www.baidu.com/s?f=8&wd=$keyworld";
    $html = file_get_contents($url);
    $html = iconv("gb2312", "utf-8//IGNORE",$html);
    print_r($html);错误:
    Warning: file_get_contents(http://www.baidu.com/s?f=8&wd=????ѹƷ ˮ֐?????) [function.file-get-contents]: failed to open stream: HTTP request failed! in E:\PHPnow\htdocs\test2\index2.php on line 6
      

  8.   

    空格问题解决了,我用$keyworld = str_replace(' ','%20',$keyworld);转换一下就可以了,谢谢大家