php文章类容分页

写的一个简单的文章内容分页类~~~
<?php
class contentPage {

private $content;
private $pageSize;
private $breakFlag;
private $url;
private $pageVar;
//参数依次为:内容, 每页的大小, 分页的标志(默认为换行符), 页面(默认为当前页),页变量(p)
public function __construct($content = "", $pageSize = 10, $breakFlag = "\n", $url = '', $pageVar = 'p') {
$this->content = $content;
$this->pageSize = $pageSize;
$this->breakFlag = $breakFlag;
$this->url = $url;
$this->pageVar = $pageVar;
}

public function page() {

$contentLength = strlen($this->content);
$i = 0;
$pageBreak[0] = 0;
$offset = $this->pageSize;
for ($j = 0; $j < $contentLength/$this->pageSize + 1; $j++) {
if ($offset > $contentLength) {
$i++;
$pageBreak[$i] = $contentLength;
break;
}
//找第$i+1页的分页位置
$where = strpos($this->content, $this->breakFlag, $offset);
if ($where > $contentLength or intval($where) < 1) {
$i++;
$pageBreak[$i] = $contentLength;
break;
}
else {
$i++;
$pageBreak[$i] = $where;
$offset = $where + $this->pageSize;
}
}
$pageCount = $i;
if (isset($_GET[$this->pageVar])) {
$page = $_GET[$this->pageVar];
}
else {
$page = 1;
}
//当page>1时,要去掉上一页的换行符
if ($page > 1) {
echo substr($this->content, $pageBreak[$page-1] + 1, $pageBreak[$page] - $pageBreak[$page-1]);
}
else {
echo substr($this->content, $pageBreak[$page-1], $pageBreak[$page] - $pageBreak[$page-1]);
}
echo "<br />";
if ($page > 1) {
echo "<a href=".$this->url."?".$this->pageVar."=".($page-1)." >上一页</a>";
}
//循环显示所有页
if ($pageCount > 1) {
for ($k = 1; $k < $pageCount + 1; $k++) {
if ($k == $page) {
echo $k . " 页";
}
else {
echo "<a href=".$this->url."?".$this->pageVar."=".$k." >" . $k . "页</a>";
}
}
}
if ($pageCount > 1 && $page < $pageCount) {
echo "<a href=".$this->url."?".$this->pageVar."=".($page+1)." >下一页</a>";
}
}
}


//用例
$content="第一页:的法定风波不断分变得疯狂八大咖啡杯思考的大部分卡布的反馈
第2页:大幅波动放开吧地方吧看不到防空部队
第3页:病毒库不哭不哭不得不对所
第4页:的泛滥的法律女的地方拉动废了你";
$cont = new contentPage($content);
$cont->page();
//echo substr($content, 77 , 128);
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值