首頁 php教程 PHP源码 长文章分页显示类,暂不支持文章中带图片,仅纯文字

长文章分页显示类,暂不支持文章中带图片,仅纯文字

May 25, 2016 pm 05:11 PM

1. [文件]     article.php 

                           

<?php
/**
 * 文章分页类
 *
 * @author ymz lymz.86@gmail.com
 * @version 1.3
 */

/*
For example:

$curPage = $_GET[&#39;page&#39;];
$totalPage = $curPage;
$objArt = new article($article,40,14);
$objArt->setWrapSign("<br/>");
$objArt->setMore(&#39;more&#39;,&#39;this is complete url address.&#39;);
$part = $objArt->getOnePage($curPage,$totalPage);
echo $part;
*/

class article {
    var $string = &#39;&#39;;
    var $curPart = &#39;&#39;;
    var $allLine = 0;
    var $totalPage = null;
    var $curPage = 1;
    var $curLine = 1;
    var $onePageLine = 10;
    var $oneLineWord = 40;
    var $arrArticle = array();
    var $wrapSign = "\n";
    var $more = null;

    function article($str,$oneLineWord = 40,$onePageLine = 10)
    {
        $this->string = $str;
        $this->oneLineWord = $oneLineWord;
        $this->onePageLine = $onePageLine;
    }

    //设置换行符
    function setWrapSign($str)
    {
        $this->wrapSign = $str;
    }

    //是否显示更多
    function setMore($str,$link)
    {
        $this->more = array(&#39;word&#39; => $str,&#39;url&#39; => $link);
    }

    //设置总页面、总行数,构造文章数组
    function getAllPage($str=&#39;&#39;)
    {
        if ($str)
            $this->string = $str;
        $this->allLine = 0;

        $arrayStr = explode($this->wrapSign,$this->string);
        foreach ($arrayStr as $num => $part) {
            $length = $this->strlen($part);
            if ($length > $this->oneLineWord) {
                $tmp_line = ceil($length / $this->oneLineWord);
            } else {
                $tmp_line = 1;
            }
            $this->allLine += $tmp_line;
            $this->arrArticle[$num] = array(
                &#39;lines&#39; => $tmp_line,
                &#39;string&#39; => $part
            );
        }
        $this->totalPage = ceil($this->allLine / $this->onePageLine);
        return $this->totalPage;
    }

    //获取某页数据
    function getOnePage($page,&$totalPage)
    {
        if (is_null($this->totalPage))
            $this->getAllPage();
        $totalPage = $this->totalPage;
        !$page && $page = 1;
        $page > $totalPage && $page = $totalPage;
        $this->curPage = $page;

        $startLine = ($page - 1) * $this->onePageLine + 1;
        $endLine = $startLine + $this->onePageLine - 1;
        $tmp_line = 0; $tmp_str = &#39;&#39;; $key = count($this->arrArticle);
        foreach ($this->arrArticle as $k => $part) {
            $tmp_line += $part[&#39;lines&#39;];
            
            if ($startLine > 1 && $tmp_line - $part[&#39;lines&#39;] < $startLine && $tmp_line >= $startLine)
            {
                $tmp_str = &#39;&#39;;
                $startWord = ($startLine - ($tmp_line - $part[&#39;lines&#39;] + 1)) * $this->oneLineWord;
                $startWord = $startWord < 0 ? 0 : $startWord;
                $needWord = $this->oneLineWord * $this->onePageLine;
                $part[&#39;string&#39;] = $this->substr($part[&#39;string&#39;],$startWord,$needWord);

                if ($tmp_line == $endLine)
                    $this->_setLastLine($part[&#39;string&#39;]);
            }
            else if ($tmp_line == $startLine - 1)
            {
                $tmp_str = &#39;&#39;;
                continue;
            }
            else if ($tmp_line > $endLine)
            {
                $needWord = ($endLine - ($tmp_line - $part[&#39;lines&#39;])) * $this->oneLineWord;
                $part[&#39;string&#39;] = $this->substr($part[&#39;string&#39;],0,$needWord);

                $this->_setLastLine($part[&#39;string&#39;]);
            }

            $tmp_str .= $part[&#39;string&#39;];
            if ($tmp_line < $endLine && $k + 1 < $key)
                $tmp_str .= &#39;<br/>&#39;;
            else if ($tmp_line >= $endLine || $k + 1 == $key) {
                $this->_setLastLine($part[&#39;string&#39;]);
                break;
            }
        }
        $this->curPart = $tmp_str;
        if (!is_null($this->more)) $this->_setMore();
        return $this->curPart;
    }

    //设置最后一行数据
    function _setLastLine($str)
    {
        $this->getAllPage($str);
        $startWord = ($this->allLine - 1) * $this->oneLineWord;
        $this->lastLine = $this->substr($str,$startWord,$this->oneLineWord);
    }

    //获取最后一行数据
    function getLastLine()
    {
        return $this->lastLine;
    }

    //对 more 进行设置
    function _setMore()
    {
        $length = $this->strlen($this->lastLine);
        if ($length + 15 > $this->oneLineWord) {
            $length = $this->strlen($this->curPart);
            $this->curPart = $this->substr($this->curPart,0,$length - 9) . &#39; ... <a href="&#39;.$this->more[&#39;url&#39;].&#39;">&#39;.$this->more[&#39;word&#39;].&#39;</a>&#39;;
        } else {
            $this->curPart .= &#39; ... <a href="&#39;.$this->more[&#39;url&#39;].&#39;">&#39;.$this->more[&#39;word&#39;].&#39;</a>&#39;;
        }
    }

    //支持中文的字符串长度获取(每2个字符表示一个中文字符)
    function strlen($str)
    {
        $length = 0;
        for($i = 0,$len = strlen($str);$i < $len; ++ $i) {
            $w = substr($str,$i,1);
            if (ord($w) > 160) {
                $length += 2;
                $i += 2;
            } else
                $length ++;
        }
        return $length;
    }

    //截取字符串,长度不足则舍弃
    function substr($str,$start,$len)
    {
        $string = &#39;&#39;;
        $length = strlen($str);
        $tmp_len = 0; $tmp_start = 0;
        for($i = 0;$i < $length; ++ $i) {
            if ($tmp_len >= $len) break;
            if ($i + 1 >= $start) {//die(&#39;,&#39;.$tmp_start);
                $w = substr($str,$tmp_start,1);
                if (ord($w) > 160) {
                    if ($tmp_len + 2 <= $len)
                        $string .= substr($str,$tmp_start,3);
                    $i += 2;
                    $tmp_len += 2;
                    $tmp_start += 3;
                } else {
                    $string .= substr($str,$tmp_start,1);
                    ++ $tmp_start;
                    ++ $tmp_len;
                }
            } else {
                $w = substr($str,$tmp_start,1);
                if (ord($w) > 160) {
                    $i += 1;
                    $tmp_start += 3;
                } else {
                    ++ $tmp_start;
                }
            }
        }
        return $string;
    }

}
登入後複製

                               

                   

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)