首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

字符串乱码解决思路

2013-06-19 
字符串乱码?php$url http://luolai.tmall.com/search.htm?pageNum1$lines_array file($url)echo

字符串乱码

<?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 

热点排行