字符串乱码解决思路
                        
                            时间:2021-07-01 10:21:17
                            帮助过:36人阅读
							                        
                     
                    
                    字符串乱码
$url = 'http://luolai.tmall.com/search.htm?pageNum=1';  
$lines_array = file($url);  
echo $lines_array; echo "
"; 
$lines_string = implode('', $lines_array);   
eregi("(.*)", $lines_string, $head); 
echo "head:".$head;  
echo "
"; print_r($head); 
echo "
";  
echo "title:".$head[1]; 
?>
这个打印出来有乱码,如何才能不乱码?
              
                
                
                
                  
分享到:
                  
                
              ------解决方案--------------------试试这个:
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] => 
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             
    [1] => 
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             
)
title:
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com