<?php
header('content-type:text/html;charset=utf-8');
//实例化memcache
$mem=new Memcache();
//链接
$mem->connect('127.0.0.1','11211');
$list=$mem->get('ceshi');
if(empty($list)){
$url="http://auto.baidu.com/";
$str=file_get_contents($url);
// echo $str;
$reg1='#<li class="sticky">.*</ul>.*</div>#isU';
preg_match($reg1,$str,$data);
// var_dump($data);
$reg2='#<li class="list"><h5><a href=".*" target="_blank" mon="a=1&p=1&z=daogou&pn=.*">(.*)</a></h5></li>#isU';
preg_match_all($reg2,$data[0],$arr1);
// var_dump($arr1);
unset($arr1[0]);//存入数据
$mem->set('ceshi',$arr1);
}
$list=$mem->get('ceshi');
// var_dump($list);die;
// $res=$list[0];
// var_dump($res);
$res=array();
foreach ($list as $key => $value) {
// var_dump($value);
$res=$value;
}
//var_dump($res);die;
//计算总条数
$count=count($res);
//设置每页显示条数
$length=3;
//总页数
$pagenum=ceil($count/$length);
// echo $pagenum;die;
$page=isset($_GET['page']) ? $_GET['page']:1;
//上一页
$prev=$page<1 ? 1:$page-1;
//下一页
$next=$page>$pagenum ? $pagenum:$page+1;
//设置偏移量
$offset=($page-1)*$length;
$list=array_slice($res,$offset,$length);
// mb_substr($list,);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
</head>
<body>
<table>
<tr>
<td>编号</td>
<td>新闻</td>
</tr>
<?php foreach($list as $k=>$v): ?><!-- mb_substr($v,'utf-8'),'.....' -->
<tr>
<td><?php echo $k+1; ?></td>
<td><?php echo mb_convert_encoding($v,'utf8','gb2312');?></td>
</tr>
<?php endforeach ?>
<tr>
<td>
<a href="qu_1">首页</a>
<!-- <a href="qu_<?php echo $prev; ?>">上一页</a>
<a href="qu_<?php echo $next; ?>">下一页</a>
<a href="qu_<?php echo $pagenum; ?>">尾页</a> -->
<a href="qu_<?php echo $prev; ?>">上一页</a>
<a href="qu_<?php echo $next; ?>">下一页</a>
<a href="qu_<?php echo $pagenum; ?>">尾页</a>
</td>
</tr>
</table>
</body>
</html>