热搜:NVER node 开发 php

字符串乱码

2024-09-12 14:15:01
字符串乱码

<?php$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]; ?>

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


回复讨论(解决方案)

试试这个:<?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);

楼上三位的都取不出字符串,显示为空白

用iconv函数或者mb_convert_encoding函数转换整个网页file_get_contents显示不出乱码,但是用正则就是无法取出中间的中文字符串。

虽然有 Deprecated:  Function eregi() is deprecated in Array 错误警告
但结果还是有的
Array
(
    [0] => 

<br>                         宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             
    [1] => 
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com             
)

title:
                        宝贝列表页-罗莱家纺官方旗舰店--            天猫Tmall.com 

嗯 这个问题已经解决了,是我正则表达式用~匹配的,换成/就没报错。再次感谢几位帮助。