class contentpage { private $content; //文章內容 private $pagesize; //每頁最少位元組數 private $breakflag; //分頁符(可以自訂,預設為) private $pageurl; //url地址 private $pagevar; //分頁參數 public $pagecount; //總頁數 public $page; //當前頁碼 public $pagebreak; //每頁起始位置 function __construct($content = "",$pagesize = 10,$breakflag =" ",$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;$k<$contentlen/$this->pagesize;$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) < 1) { $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] <= $this->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 = "<a href='".$this->geturl()."=".($this->page-1)."' class='div'>上一頁</a> "; } //輸出數字頁碼 for($j=1;$j<=$this->pagecount;$j++) { if($j == $this->page) { $pagenav .= "<span class='divsi'>".$j."</span> "; } else { $pagenav .= "<a href='".$this->geturl()."=".$j."' class='div'>".$j."</a> "; } } //下一頁 if($this->page < $this->pagecount && $this->pagecount >1) { $pagenav .= "<a href='".$this->geturl()."=".($this->page+1)."' class='div'>下一頁</a> "; } 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(); //輸出頁碼 ?> <style type="text/css教程"> <!-- body { width:800px; margin:0 auto; margin-top:50px; font-size:12px; } a { color:#014ccc; text-decoration:none; } .div { float:left; cursor:pointer; font-weight:bold; margin-right:5px; display: block; padding:3px 7px; text-align:center; border:#bbdded solid 1px; } .divs { float:left; font-weight:bold; margin-right:5px; display: block; padding:3px 7px; text-align:center; border:#cccccc solid 1px; } .divsi { float:left; font-weight:bold; margin-right:5px; display: block; padding:3px 7px; text-align:center; background:#3399ff; color:#ffffff; border:#cccccc solid 1px; } .div:hover { background:#3399ff; color:#ffffff; } .divsi:hover { background:#3399ff; color:#ffffff; } |