php 文章内容分页代码_PHP教程
php教程 文章内容分页代码,是根据由编辑器插入的分页符来操作了,我们可以分出超漂亮的偏移效果的分页。
文章内容分页
文章内容分页
* author:陈凯
* data:2010-09-15
* 文章分页类
*/
class contentpage
{
private $content; //文章内容
private $pagesize; //每页最少字节数
private $breakflag; //分页符(可以自定义,默认为n)
private $pageurl; //url地址
private $pagevar; //分页参数
public $pagecount; //总页数
public $page; //当前页码
public $pagebreak; //每页起始位置function __construct($content = "",$pagesize = 10,$breakflag ="n",$pageurl = '',$pagevar = 'p')
{
$this->content = $content;
$this->pagesize = $pagesize;
$this->breakflag = $breakflag;
$this->pageurl = $pageurl;
$this->pagevar = $pagevar;
$this->getpages();
}//总页数,每页的起始位置和结束位置
public function getpages()
{
$contentlen = strlen($this->content); //文章总字节数
$this->pagebreak[0] = 0;
$i = 0;
$offset = $this->pagesize;
for ($k=0;$kpagesize;$k++)
{
if($offset > $contentlen)
{
$i++;
$this->pagebreak[$i] = $contentlen;
break;
}
//查找$this->pagevar出现的位置
$where = strpos($this->content,$this->breakflag,$offset);
if($where > $contentlen or intval($where) {
$i++;
$this->pagebreak[$i] = $contentlen;
break;
}
else
{
$i++;
$this->pagebreak[$i] = $where;
$offset = $where + $this->pagesize;
}
}
$this->pagecount = $i;
if(isset($_get[$this->pagevar]) && $_get[$this->pagevar] >1 && $_get[$this->pagevar] pagecount)
{
$this->page = $_get[$this->pagevar];
}
else
{
$this->page = 1;
}
}//每页内容
function getpage()
{
//截取当前页码的数据
if($this->page > 1)
{
return substr($this->content,$this->pagebreak[$this->page-1]+1,$this->pagebreak[$this->page] - $this->pagebreak[$this->page-1]);
}
else
{
return substr($this->content,$this->pagebreak[$this->page-1],$this->pagebreak[$this->page] - $this->pagebreak[$this->page-1]);
}}
//分页条
public function getpagenav()
{
if($this->page > 1)
{
$pagenav = "上一页 ";
}//输出数字页码
for($j=1;$jpagecount;$j++)
{
if($j == $this->page)
{
$pagenav .= "".$j." ";
}
else
{
$pagenav .= "".$j." ";
}
}
//下一页
if($this->page pagecount && $this->pagecount >1)
{
$pagenav .= "下一页 ";
}
return $pagenav;
}
//获取url地址
public function geturl()
{
$url = $_server['request_uri'];
$parse_url = parse_url($url);
$query_url = $parse_url['query'];
if($query_url)
{
$query_url = ereg_replace("(^|&)".$this->pagevar."=".$this->page,"",$query_url);
$url = str_replace($parse_url['query'],$query_url,$url);
if($query_url)
{
$url .= "&".$this->pagevar;
}
else
{
$url .= $this->pagevar;
}
}
else
{
$url .= "?".$this->pagevar;
}
return $url;
}
}$content = "第一页:文章内容分页阿斯顿浪费空间阿斯顿来看福建省地方吉林省福建路口附近大手拉飞机上浪费的说浪费监理费
第二页:阿斯顿房间阿双方了解啊对萨拉开发记得谁来付款将令对方空间的来福建阿里是否
第三页:欧文炯诶哦生地拉开方面来看就继续超文章内容分页滤机蓝卡
第四页:欧文日据拉萨及发动四分啊就双方的将爱是发觉是文章内容分页否了。";
$model = new contentpage($content);
echo $model->getpage(); //输出分页内容
echo $model->getpagenav(); //输出页码
?>
-->