php 长文章分页显示类的完整代码

内容摘要
这篇文章主要为大家详细介绍了php 长文章分页显示类的完整代码,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:

/**
*
文章正文

这篇文章主要为大家详细介绍了php 长文章分页显示类的完整代码,具有一定的参考价值,可以用来参考一下。

对此感兴趣的朋友,看看idc笔记做的技术笔记。经测试代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<code class="php">
/**
 * 文章分页类
 *
 * @param
 * @author php教程 www.idcnote.com
 **/
/*
For example:
$curPage = $_GET['page'];
$totalPage = $curPage;
$objArt = new article($article,40,14);
$objArt->setWrapSign("<br/>");
$objArt->setMore('more','this is complete url address.');
$part = $objArt->getOnePage($curPage,$totalPage);
echo $part;
*/
class article {
    var $string = '';
    var $curPart = '';
    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('word' => $str,'url' => $link);
    }
    //设置总页面、总行数,构造文章数组
    function getAllPage($str='')
    {
        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(
                'lines' => $tmp_line,
                'string' => $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 = ''; $key = count($this->arrArticle);
        foreach ($this->arrArticle as $k => $part) {
            $tmp_line += $part['lines'];
             
            if ($startLine > 1 && $tmp_line - $part['lines'] < $startLine && $tmp_line >= $startLine)
            {
                $tmp_str = '';
                $startWord = ($startLine - ($tmp_line - $part['lines'] + 1)) * $this->oneLineWord;
                $startWord = $startWord < 0 ? 0 : $startWord;
                $needWord = $this->oneLineWord * $this->onePageLine;
                $part['string'] = $this->substr($part['string'],$startWord,$needWord);
                if ($tmp_line == $endLine)
                    $this->_setLastLine($part['string']);
            }
            else if ($tmp_line == $startLine - 1)
            {
                $tmp_str = '';
                continue;
            }
            else if ($tmp_line > $endLine)
            {
                $needWord = ($endLine - ($tmp_line - $part['lines'])) * $this->oneLineWord;
                $part['string'] = $this->substr($part['string'],0,$needWord);
                $this->_setLastLine($part['string']);
            }
            $tmp_str .= $part['string'];
            if ($tmp_line < $endLine && $k + 1 < $key)
                $tmp_str .= '<br/>';
            else if ($tmp_line >= $endLine || $k + 1 == $key) {
                $this->_setLastLine($part['string']);
                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) . ' ... <a href="'.$this->more['url'].'">'.$this->more['word'].'</a>';
        } else {
            $this->curPart .= ' ... <a href="'.$this->more['url'].'">'.$this->more['word'].'</a>';
        }
    }
    //支持中文的字符串长度获取(每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 = '';
        $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(','.$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;
    }
}
 For example:
 $curPage = $_GET['page'];
 $totalPage = $curPage;
 $objArt = new article($article,40,14);
 $objArt->setWrapSign("<br/>");
 $objArt->setMore('more','this is complete url address.');
 $part = $objArt->getOnePage($curPage,$totalPage);
 echo $part;
 
 
/***   代码来自php教程(www.idcnote.com)   ***/</code>

注:关于php 长文章分页显示类的完整代码的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!