页面静态化

<?php
    header("content-type:text/html;charset=utf8");
    set_time_limit(0);
    $url = "http://blog.jobbole.com/tag/php/";
    $str = file_get_contents($url);
    // var_dump($str);die;
    $reg = '#<!-- BEGIN .post -->.*<!-- END .post -->#isU';
    preg_match_all($reg,$str,$arr);
    // var_dump($arr);die;view-source:http://errorpage.b0.upaiyun.com/jbcdn2-403
    for ($j=0; $j < count($arr[0]); $j++) {
        // 匹配图片
        $reg_img = '#<img src="(.*)".* />#isU';
        preg_match($reg_img,$arr[0][$j],$arr_img);
        // var_dump($arr_img);die;
        // 将图片缓存到本地
        /*if (@fopen($arr_img[1],"r")) {
            // $img_data = $arr_img[1];
            $img = file_get_contents($arr_img[1]);
            $suf = substr($arr_img[1],strrpos($arr_img[1],'.'));
            $file_name = "images/".time().rand(000000,999999).$suf;
            file_put_contents($file_name,$img);
        }else{
            $file_name = "";
        }*/
        // 匹配标题
        $reg_title = '#<a target="_blank" class="archive-title" href=".*" title="(.*)">.*</a>#isU';
        preg_match($reg_title,$arr[0][$j],$arr_title);
         // var_dump($arr_title);die;
        // 匹配内容
        $reg_content = '#<span class="excerpt"><p>(.*)</p>.*</span>#isU';
        preg_match($reg_content,$arr[0][$j],$arr_content);
        // var_dump($arr_content);die;
        // 匹配时间
        $reg_time = '#<br />(.*)<a href=".*">#isU';
        preg_match($reg_time,$arr[0][$j],$arr_time);
        // 开启pdo扩展
        $pdo = new PDO("mysql:host=127.0.0.1;dbname=demo","root","root");
        $pdo->exec("set names utf8");
        // 将标题 内容 时间  图片  入库
        $arr_title = $arr_title[1];
        $arr_content = $arr_content[1];
        $arr_time = $arr_time[1];
        /*$sql = "insert into ymjth (arr_img,arr_title,arr_content,arr_time) values('$file_name','$arr_title','$arr_content','$arr_time')";
        $pdo->exec($sql);*/
                //展示
        $sum = 80;
        $sql_look = "select * from ymjth limit $sum";
        $preObj = $pdo->prepare($sql_look);
        $preObj->execute(array(20));
        $look = $preObj->fetchAll(PDO::FETCH_ASSOC);
    }
            //搜索后分页
    $sou = isset($_REQUEST['sou']) ? $_REQUEST['sou'] : '';
            //分页(PHP)
    $page = isset($_GET['page']) ? $_GET['page'] : 1;//当前页
    // echo $page;die;
    $counts = count($pdo->query("select * from ymjth where arr_title like '%$sou%'")->fetchAll(PDO::FETCH_ASSOC));//总条数
    $length = 3;
    $pages = ceil($counts/$length);//总页数
    // echo $pages;die;
    //页码
    $prev = $page<=1 ? 1 : $page-1;//上一页
    $next = $page>=$pages ? $pages : $page+1;//下一页
    $offset = ($page-1)*$length;//偏移量
    // echo $offset;die;
    $data = $pdo->query("select * from ymjth where arr_title like '%$sou%' limit $offset,$length")->fetchAll(PDO::FETCH_ASSOC);
    // var_dump($data);die;
 ?>
 <center>
     <form action="" method="post">
         <input type="text" name="sou" value="<?php echo $sou ?>" />
         <input type="submit" value="搜索" />
     </form>
     <table border="1">
         <tr>
             <th>编号ID</th>
             <th>图片</th>
             <th>标题</th>
             <th>内容</th>
             <th>时间</th>
         </tr>
         <?php foreach ($data as $key => $value): ?>
             <tr>
                 <td><?php echo $value['id'] ?></td>
                 <td><img src="demo/9.16/2/<?php echo $value['arr_img'] ?>" style="width: 100px;height: 80px" ></td>
                 <td><?php echo $value['arr_title'] ?></td>
                 <td><?php echo $value['arr_content'] ?></td>
                 <td><?php echo $value['arr_time'] ?></td>
             </tr>
         <?php endforeach ?>
    </table>
    <a href="1_<?php echo urlencode($sou) ?>.html">首页</a>
    <a href="<?php echo $prev; ?>_<?php echo urlencode($sou) ?>.html">上一页</a>
    <a href="<?php echo $next ?>_<?php echo urlencode($sou) ?>.html">下一页</a>
    <a href="<?php echo $pages ?>_<?php echo urlencode($sou) ?>.html">末页</a>
</center>



#开启重写引擎
RewriteEngine on
#配置重写规则
RewriteRule  a.html      demo/9.16/2/a.php [L,NC]
#分页的伪静态配置
RewriteRule  (\d+).html      demo/9.16/2/a.php?page=$1 [L,NC]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值