file_get_contents 采集一个页面的数据,获取的数据是乱码,已经使用了检测编码的方式,
检测的是utf-8,我的页面编码也是utf-8,但是还是显示乱码,不知道为什么
$url=xxx;$opts = array( 'http'=>array( 'user_agent' => mozilla/4.0 (compatible; msie 5.01; windows nt 5.0), ) ); $context = stream_context_create($opts); $neirong = file_get_contents($url, false, $context); header(content-type: text/html; charset=utf-8); ob_end_flush(); $encode = mb_detect_encoding($neirong, array(ascii,utf-8,gb2312,gbk,big5)); echo $encode.
; if ($encode!=utf-8) { $neirong=mb_convert_encoding($neirong, utf-8, $encode); } echo $neirong;
$encode 输出:utf-8
$neirong 输出是乱码
我的页面编码是utf-8
回复讨论(解决方案) ...$neirong = file_get_contents($url, false, $context);echo base64_encode($neirong);
贴出结果
...$neirong = file_get_contents($url, false, $context);echo base64_encode($neirong);
贴出结果
是一篇文章,结果太长了,我贴一段吧
77u/icagioiwjeewloahteayloazhoixkeebqoaak++8jomaves6juwhkes+k+wyqeegmueeg+ebqoa0headremav
$c = '77u/icagioiwjeewloahteayloazhoixkeebqoaak++8jomaves6juwhkes+k+wyqeegmueeg+ebqoa0headremav';echo base64_decode($c);
谍疔?昔,?于凑?岩杭
?有啊。最後那?是因?你的base64 不完整。
$c = '77u/icagioiwjeewloahteayloazhoixkeebqoaak++8jomaves6juwhkes+k+wyqeegmueeg+ebqoa0headremav';echo base64_decode($c);
谍疔?昔,?于凑?岩杭
?有啊。最後那?是因?你的base64 不完整。
正确的输出应该是 “让田树新无语的是,这个六伯居然真的没有这方面的意思。 ”
是乱码啊
把?集的地址?出?。
把?集的地址?出?。
这个是采集数据地址
http://www.ziyouge.com/conbdhekbefiab
这个是它网站的显示页面
http://www.ziyouge.com/zy/4/4980/1333249.html
采集地址的数据不正常,但它的页面显示正常
需要集的做些?理才行。
12) ) . chr ( 0x80 | (($val >> 6) & 0x3f) ) . chr ( 0x80 | ($val & 0x3f) ); $i += 5; } else if ($str [$i] == '%') { $ret .= urldecode ( substr ( $str, $i, 3 ) ); $i += 2; } else $ret .= $str [$i]; } return $ret; } ?>
已经是晚上十一点了,路边的拍档基本?什么人了,不过还有三货依然喝的兴高采烈的,而且大有不干到天明,誓不罢休的意思,
fdipzone ,用你的方法还是输出的乱码,对解密不太了解
你在?出的html中加入
它源是做了些的,我那?程序已?是了。
我把?集的也?出?,直接?行就可以了。
$v ) { $headerarr[] = $n .':' . $v; }$ch = curl_init();curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_httpheader , $headerarr ); //构造ipcurl_setopt($ch, curlopt_referer, 'http://www.ziyouge.com/'); //构造来路$content = curl_exec($ch);$content = substr($content,3);if($error=curl_error($ch)){ die($error);}curl_close($ch);// 分析程序$result = '';$str_length = mb_strlen($content);$i=0;while ($i=224){ $result .= change(mb_substr($content,$i,3)); $i=$i+3; }else{ $result .= mb_substr($content,$i,1); $i=$i+1; }}echo '';echo $result;// ?理function change($str){ $ignore = array('“','”','!','…',':',',',','); if(in_array($str, $ignore)){ return $str; } $prefix = %u; $postfix = ; $str = iconv('utf-8', 'ucs-2', $str); $arrstr = str_split($str, 2); $unistr = ''; for($i = 0, $len = count($arrstr); $i < $len; $i++) { $tmp = hexdec(bin2hex($arrstr[$i])); $tmp = str_pad(dechex($tmp),4,'0',str_pad_left); $tmp = decrypt(substr($tmp,2,2).substr($tmp,0,2)); $unistr .= $prefix . $tmp . $postfix; } return unescape($unistr);}// 解密function decrypt($d){ $result = str_pad(dechex(hexdec($d)-100),4,'0',str_pad_left); return $result;}// ?中文function unescape($str) { $ret = ''; $len = strlen ( $str ); for($i = 0; $i > 12) ) . chr ( 0x80 | (($val >> 6) & 0x3f) ) . chr ( 0x80 | ($val & 0x3f) ); $i += 5; } else if ($str [$i] == '%') { $ret .= urldecode ( substr ( $str, $i, 3 ) ); $i += 2; } else $ret .= $str [$i]; } return $ret; } ?>
你在?出的html中加入
它源是做了些的,我那?程序已?是了。
我把?集的也?出?,直接?行就可以了。
[/code]
出现乱码是因为php版本不同,我在5.3.28测试正常,在php 6.0.0-dev 中测试就是乱码,是不是php 6.0.0-dev缺少了什么组件
可能吧,dev版。。
可能吧,dev版。。
本地 5.3.28正常,换到服务器5.3.28又出现乱码了。。。
liunx的环境 ,本地是 ubuntu,服务器是debian
估php mb string 的版本有?。
?境只能靠你自己?理了,我?有多?境。
估php mb string 的版本有?。
?境只能靠你自己?理了,我?有多?境。
已经发现问题所在。不同平台下
$str = iconv('utf-8', 'ucs-2', $str); //这句输出的结果不同
//例如:$str=?; $str = iconv('utf-8', 'ucs-2', $str);正常的结果是“v^”;不正常的结果是“^v”;请教这个该怎么解决
找到方法了。。不同平台转换的usc-2编码不同
对于 ucs-2, linux 下默认是 ucs-2be。用iconv(指定ucs-2)来转换生成的是 ucs-2be 的 unicode。如果转换windows平台过来的 ucs-2, 需要指定 ucs-2le。
嗯
$str = iconv('utf-8', 'ucs-2', $str);
改?
$str = iconv('utf-8', 'ucs-2le', $str);
就可以了。