011PHP文件处理——文件处理 文件内容分页操作类

<?php

/**
 * 文件内容分页操作类:
 */
//访问地址:http://basicphp.com/006file/011.php?&page=1
class StrPage
{
    private $file;//文件内容
    private $current;//当前页
    private $totalPage;//总的页数
    private $url;
    private $pageLen;//每一页显示的内容长度

    function __construct($file, $len = 90)
    {
        $this->file = file_get_contents($file);
        $this->current = isset($_GET['page']) ? $_GET['page'] : 1;
        $this->pageLen = $len;
        $this->totalPage = $this->getTotalPage();
        $this->url = $this->getUrl();
        //echo $this->url;
    }

    private function getTotalPage()
    {
        return ceil(strlen($this->file) / $this->pageLen);
    }

    private function getUrl()
    {
        /*
         * $_SERVER['QUERY_STRING']  查询(query)的字符串:输出:page=3&cid=3
           $_SERVER['PHP_SELF']; 获取当前页;输出:011.php
            $_SERVER['REQUEST_URI']; 获取当前页,在获取查询的字符串
        */
        $url = parse_url($_SERVER['REQUEST_URI']);
        //print_r($url);//输出:Array ( [path] => /006file/011.php [query] => page=3&cid=3 )

        parse_str($url['query'], $queryArr);//parse_str();把查询字符串解析到变量中
        //print_r($queryArr);//输出:Array ( [page] => 3 [cid] => 5 )
        unset($queryArr['page']);//输出数组中的page参数
        $queryStr = http_build_query($queryArr);// http_build_query()  生成 URL-encode 之后的请求字符串
        return $url['path'] . '?' . $queryStr . '&page=';//返回:/006file/011.php?cid=5&page=
    }

    private function first()
    {
        if ($this->current > 1) return "<a href='" . $this->url . "1'>首页</a>";
    }

    private function pre()
    {
        if ($this->current > 1) return "<a href='" . $this->url . ($this->current - 1) . "'>上一页</a>";
    }

    private function next()
    {
        if ($this->current < $this->totalPage) return "<a href='" . $this->url . ($this->current + 1) . "'>下一页</a>";
    }

    private function end()
    {
        if ($this->current < $this->totalPage) return "<a href='" . $this->url . $this->totalPage . "'>末页</a>";
    }

    private function pageList()
    {
        $pageListStr = '';
        for ($i = 1; $i < $this->totalPage; $i++) {
            if ($i == $this->current) {
                $pageListStr .= $i;
            } else {
                $pageListStr .= "<a href='" . $this->url . $i . "'>$i"."  </a>";
            }

        }
        return $pageListStr;
    }

    public function pageStyle($style = 1)
    {
        switch ($style) {
            case 1:
                return "共有" . $this->totalPage . "页" . $this->first() . $this->pre() . $this->pageList() . $this->next() . $this->end();
                break;
            case 2:
                return $this->pageList();
        }
    }

    public function getContents()
    {

        $prePageLen = strlen($this->subStrs($this->current - 1));
        $currentPageLen = strlen($this->subStrs($this->current));
        return substr($this->file,$prePageLen,$currentPageLen-$prePageLen);
       // return 22;
    }

    private function subStrs($page)
    {
        $len = $page * $this->pageLen;
        $string = '';
        for ($i = 0; $i < $len; $i++) {
            if (ord(substr($this->file, $i, 1)) > 0xa0) {
                $string .= substr($this->file, $i, 2);
            } else {
                $string .= substr($this->file, $i, 1);
            }
        }
        return $string;
    }
}

$php = new StrPage('a.txt',90);
echo '<div style="font-size: 12px; color: green;height: 80px;">'.$php->getContents().'</div>';
echo "<div style='border: 1px #188eee solid; font-size: 12px;'>";
echo $php->pageStyle();
echo "</div>";

  

原文地址:https://www.cnblogs.com/yiweiyihang/p/8297845.html