我認爲一個還算不錯的長文章分頁類

  1. <?php
  2. /*
  3. *長文章分頁類
  4. *author tdweb@GGT
  5. *date 2008-8-4
  6. *$content 要拆分的文章內容
  7. */
  8. Class cutContent{
  9.     public $content;
  10.     public $limit;
  11.     public $length;
  12.     public $pageCount;
  13.     public $nowPage;
  14.     public $contentLeft;
  15.     public $contentRight;
  16.     public $array_content;
  17.     public $page_array_content;
  18.     public $page_tag='#p#';
  19.     function cutContent() {
  20.         $this->limit=270;
  21.         $this->nowPage = $_REQUEST['pagec']?$_REQUEST['pagec']:1;
  22.     }
  23.     /*
  24.     *需要用到的字符串截取方法
  25.     */
  26.     function msubstr($str,$start,$len)
  27.     {
  28.         $strlen = $start +$len;
  29.         for($i=0;$i<$strlen;$i++)
  30.         {
  31.             if(ord(substr($str,$i,1))>0xa0)
  32.             {
  33.                 $temstr .= substr($str,$i,2);
  34.                 $i++;
  35.             }
  36.             else
  37.             {
  38.                 $temstr .=substr($str,$i,1);
  39.             }
  40.         }
  41.         return $temstr;
  42.     }
  43.     /*
  44.     *獲取當前頁內容
  45.     */
  46.     function getPerContent(){
  47.         if (strpos($this->content,$this->page_tag) > -1){
  48.             $arr_all_content=explode($this->page_tag,$this->content);
  49.             $this->pageCount=count($arr_all_content);
  50.             if($this->pageCount<$this->nowPage) $this->nowPage = 1;
  51.             $this->contentLeft=$arr_all_content[$this->nowPage-1];
  52.             $this->contentRight=$arr_all_content[$this->nowPage+1];
  53.             $this->array_content=$arr_all_content[$this->nowPage-1];
  54.             return $this->array_content;
  55.         }else{
  56.             $this->length=strlen($this->content);
  57.             $this->pageCount=ceil($this->length/$this->limit);
  58.             if($this->pageCount<$this->nowPage) $this->nowPage = 1;
  59.             $this->contentLeft = $this->msubstr($this->content,0,($this->nowPage-1)*$this->limit);
  60.             $this->contentRight= $this->msubstr($this->content,0,$this->nowPage*$this->limit);
  61.             $this->array_content = substr($this->contentRight,strlen($this->contentLeft),strlen($this->contentRight)-strlen($this->contentLeft));
  62.             $this->page_array_content=strlen($this->array_content);
  63.             return $this->array_content;
  64.         }
  65.     }
  66.     /*
  67.     *獲取分頁列表
  68.     */
  69.     function getPages(){
  70.         $result="";
  71.         if (1==$this->getAllCount()){
  72.             $result='<div id="footer_nopage"> <a class="prev" style="background-color:#000;filter:Alpha(Opacity=60);"></a> <a class="next" style="background-color:#000;filter:Alpha(Opacity=60);"></a> <a class="home" href="index.php" style="background-color:#000;filter:Alpha(Opacity=60);"></a> </div>';
  73.         }else{
  74.             $result = '<div id="footer">';
  75.             if ($this->nowPage>1){
  76.                 $result .= '<a class="prev" style="background-color:#000;filter:Alpha(Opacity=60);" href="javascript:goPage2(' . ($this->nowPage-1) . ',/'' . $base_url .'/')"></a>';
  77.             }else{
  78.                 $result .= '<a class="prev" style="background-color:#000;filter:Alpha(Opacity=60);"></a>';
  79.             }
  80.             if ($this->nowPage==$this->pageCount){
  81.                 $result .= '<a class="next" style="background-color:#000;filter:Alpha(Opacity=60);"></a>';
  82.             }else{
  83.                 $result .= '<a class="next" style="background-color:#000;filter:Alpha(Opacity=60);" href="javascript:goPage2(' . ($this->nowPage + 1) . ',/'' . $base_url . '/')"></a>';
  84.             }
  85.             $result .= '<a class="home" style="background-color:#000;filter:Alpha(Opacity=60);" href="/index.php"></a> ';
  86.             $result .= ' </DIV>';
  87.         }
  88.         return $result;
  89.     }
  90.     /*
  91.     *獲取當前頁字符數
  92.     */
  93.     function getNowCount(){
  94.         return strlen($this->getPerContent());
  95.     }
  96.     /*
  97.     *獲取當前頁碼
  98.     */
  99.     function getNowPage(){
  100.         return $this->nowPage;
  101.     }
  102.     /*
  103.     *獲取頁碼總數
  104.     */
  105.     function getAllCount(){
  106.         if (strpos($this->content,$this->page_tag) > -1){
  107.             $arr_all_content=explode($this->page_tag,$this->content);
  108.             $this->pageCount=count($arr_all_content);
  109.             return $this->pageCount;
  110.         }else{
  111.         $this->length=strlen($this->content);
  112.         $this->pageCount=ceil($this->length/$this->limit);
  113.         return $this->pageCount;
  114.         }
  115.     }
  116.     /*
  117.     *獲取當前頁-總頁數的字符串
  118.     */
  119.     function getOne2All(){
  120.         $return='';
  121.         $return = "(";
  122.         $return .= $this->getNowPage();
  123.         $return .= "/";
  124.         $return .= $this->getAllCount();
  125.         $return .= ")";
  126.         return $return;
  127.     }
  128. }
  129. ?>

 

你要用的話,分頁方式還需要再改一下。

會首先搜索分頁標籤,$page_tag,如果存在分頁標籤,則按照分頁標籤進行分頁。

如果不存在,則按照$page_limit進行分頁。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章