file_get_contents()乱码问题

FG2006 2010-07-15 03:14:25
我在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);
...全文
1629 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyiply 2011-11-22
  • 打赏
  • 举报
回复
三楼的方法可以,我试过了
huanglei814 2011-03-09
  • 打赏
  • 举报
回复
我也碰到了,想看看怎么解决的呢
战斗生活 2010-08-01
  • 打赏
  • 举报
回复
编码问题呗。。
LX425 2010-07-22
  • 打赏
  • 举报
回复
楼主的乱码是什么意思!!我遇见过英文真确,中文乱码的情况,情况不同,处理方式好像也不太一样
woshimajia0003 2010-07-22
  • 打赏
  • 举报
回复
iconv 转换编码
FG2006 2010-07-22
  • 打赏
  • 举报
回复
空格问题解决了,我用$keyworld = str_replace(' ','%20',$keyworld);转换一下就可以了,谢谢大家
FG2006 2010-07-22
  • 打赏
  • 举报
回复
用了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
Alfredknox 2010-07-16
  • 打赏
  • 举报
回复
学习中。。。帮顶
cnsillycat 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wlhcy 的回复:]

PHP code
<?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_con……
[/Quote]
caizhuguanguan 2010-07-16
  • 打赏
  • 举报
回复
顶起,接分。。。
life169 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xuzuning 的回复:]
你这样输出的话,他原来的语言声明依然起作用
<meta http-equiv="content-type" content="text/html;charset=gb2312">



echo str_replace('gb2312', 'utf-8', $html));
[/Quote]
======================================
这个是正解
chinajoy2012 2010-07-16
  • 打赏
  • 举报
回复
要制定编码转换才行啊
kyzy_yy_pm 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xuzuning 的回复:]
你这样输出的话,他原来的语言声明依然起作用
<meta http-equiv="content-type" content="text/html;charset=gb2312">



echo str_replace('gb2312', 'utf-8', $html));
[/Quote]+
igaojie 2010-07-16
  • 打赏
  • 举报
回复
把body中的内容提取出来 头部不要了不就OK!
shrimpma 2010-07-16
  • 打赏
  • 举报
回复
直接去body中的内容也可以吧
ihefe 2010-07-15
  • 打赏
  • 举报
回复
self:: 这个去掉
ihefe 2010-07-15
  • 打赏
  • 举报
回复

$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"]);
}
}
}
foolbirdflyfirst 2010-07-15
  • 打赏
  • 举报
回复
 $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);
wlhcy 2010-07-15
  • 打赏
  • 举报
回复
<?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;
?>
xuzuning 2010-07-15
  • 打赏
  • 举报
回复
你这样输出的话,他原来的语言声明依然起作用
<meta http-equiv="content-type" content="text/html;charset=gb2312">



echo str_replace('gb2312', 'utf-8', $html));
加载更多回复(1)

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧