日期:2014-05-17  浏览次数:20416 次

字符串乱码
<?php
$url = 'http://luolai.tmall.com/search.htm?pageNum=1';  
$lines_array = file($url);  
echo $lines_array; echo "<br/>"; 

$lines_string = implode('', $lines_array);   
eregi("<title>(.*)</title>", $lines_string, $head); 
echo "head:".$head;  
echo "<br/>"; print_r($head); 
echo "<br/>";  
echo "title:".$head[1]; 

?>

这个打印出来有乱码,如何才能不乱码?

------解决方案--------------------
试试这个:<?php
function file_get_contents_utf8($fn) {
$content = file_get_contents($fn);
  return mb_convert_encoding($content, 'UTF-8', 
  mb_detect_encoding($content, 'UTF-8, ISO-8859-1', true));
}
?>

------解决方案--------------------
淘宝是gb2312编码,而你的是utf-8编码,因此就乱码了。
echo "title:".iconv('gbk','utf-8',$head[1]); 
------解决方案--------------------
他的页面是 gb2312 的,而你的是 utf-8 的
所以在打印前应做编码转换
$head = iconv('gbk', 'utf-8', $head);
------解决方案--------------------
虽然有 Deprecated:  Function eregi() is deprecated in Array 错误警告
但结果还是有的
Array
(
    [0] => <title>
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             </title>
    [1] => 
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             
)
<br/>title:
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com