VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php对内容较长的文章进行分页

  1. require "page.class.php"
  2. $neirong = "内容内容内容内容内容内容内容内容内容内容内容内容内容内容"
  3. $content = file_get_contents($neirong);  
  4. $ipage = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;  
  5. $CP = new cutpage($content);  
  6. $page = $CP->cut_str();  
  7. echo $page[$ipage - 1];  
  8. echo '' . $CP->pagenav() . ''
  9.  
  10. //class类 
  11.  
  12. //page.class.php 
  13.  
  14. <?php     
  15. /*   
  16. *  长文章分页类      
  17. */   
  18.     class cutpage{     
  19.         private $pagestr;       //被切分的内容     
  20.         private $pagearr;       //被切分文字的数组格式     
  21.         private $sum_word;      //总字数(UTF-8格式的中文字符也包括)     
  22.         private $sum_page;      //总页数     
  23.         private $page_word;     //一页多少字     
  24.         private $cut_tag;       //自动分页符     
  25.         private $cut_custom;    //手动分页符     
  26.         private $ipage;         //当前切分的页数,第几页     
  27.         private $url;     
  28.           
  29.         function __construct($pagestr,$page_word=1000){     
  30.             $this->page_word = $page_word;     
  31.             $this->cut_tag = array("</table>""</div>""</p>""<br/>""”。""。"".""!""……""?"",");     
  32.             $this->cut_custom = "{nextpage}";     
  33.             $tmp_page = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;     
  34.             $this->ipage = $tmp_page>1?$tmp_page:1;  
  35.             $this->pagestr = $pagestr
  36.         }     
  37.         //统计总字数     
  38.         function get_page_word(){     
  39.             $this->sum_word = $this->strlen_utf8($this->pagestr);     
  40.             return $this->sum_word;     
  41.         }     
  42.         /*  统计UTF-8编码的字符长度   
  43.          *  一个中文,一个英文都为一个字   
  44.          */   
  45.         function strlen_utf8($str){     
  46.            $i = 0;     
  47.            $count = 0;     
  48.            $len = strlen ($str);     
  49.            while ($i < $len){     
  50.                $chr = ord ($str[$i]);     
  51.                $count++;     
  52.                $i++;     
  53.                if ($i >= $len)     
  54.                    break;     
  55.                if ($chr & 0x80){     
  56.                    $chr <<= 1;     
  57.                    while ($chr & 0x80) {     
  58.                        $i++;     
  59.                        $chr <<= 1;     
  60.                    }     
  61.                }     
  62.            }     
  63.            return $count;     
  64.         }     
  65.         //设置自动分页符号     
  66.         function set_cut_tag($tag_arr=array()){     
  67.             $this->cut_tag = $tag_arr;     
  68.         }     
  69.         //设置手动分页符     
  70.         function set_cut_custom($cut_str){     
  71.             $this->cut_custom = $cut_str;     
  72.         }     
  73.         function show_cpage($ipage=0){     
  74.             $this->cut_str();     
  75.             $ipage = $ipage ? $ipage:$this->ipage;     
  76.             return $this->pagearr[$ipage];     
  77.         }     
  78.         function cut_str(){     
  79.             $str_len_word = strlen($this->pagestr);     //获取使用strlen得到的字符总数     
  80.             $i = 0;     
  81.             if ($str_len_word<=$this->page_word){   //如果总字数小于一页显示字数     
  82.                 $page_arr[$i] = $this->pagestr;     
  83.             }else{     
  84.                 if (strpos($this->pagestr, $this->cut_custom)){     
  85.                     $page_arr = explode($this->cut_custom, $this->pagestr);     
  86.                 }else{     
  87.                     $str_first = substr($this->pagestr, 0, $this->page_word);   //0-page_word个文字    cutStr为func.global中的函数     
  88.                     foreach ($this->cut_tag as $v){     
  89.                         $cut_start = strrpos($str_first$v);       //逆向查找第一个分页符的位置     
  90.                         if ($cut_start){     
  91.                             $page_arr[$i++] = substr($this->pagestr, 0, $cut_start).$v;     
  92.                             $cut_start = $cut_start + strlen($v);     
  93.                             break;     
  94.                         }     
  95.                     }     
  96.                     if (($cut_start+$this->page_word)>=$str_len_word){  //如果超过总字数     
  97.                         $page_arr[$i++] = substr($this->pagestr, $cut_start$this->page_word);     
  98.                     }else{     
  99.                         while (($cut_start+$this->page_word)<$str_len_word){     
  100.                             foreach ($this->cut_tag as $v){     
  101.                                 $str_tmp = substr($this->pagestr, $cut_start$this->page_word);        //取第cut_start个字后的page_word个字符     
  102.                                 $cut_tmp = strrpos($str_tmp$v);       //找出从第cut_start个字之后,page_word个字之间,逆向查找第一个分页符的位置     
  103.                                 if ($cut_tmp){     
  104.                                     $page_arr[$i++] = substr($str_tmp, 0, $cut_tmp).$v;     
  105.                                     $cut_start = $cut_start + $cut_tmp + strlen($v);     
  106.                                     break;     
  107.                                 }     
  108.                             }       
  109.                         }     
  110.                         if (($cut_start+$this->page_word)>$str_len_word){     
  111.                             $page_arr[$i++] = substr($this->pagestr, $cut_start$this->page_word);     
  112.                         }     
  113.                     }     
  114.                 }     
  115.             }     
  116.             $this->sum_page = count($page_arr);     //总页数     
  117.             $this->pagearr = $page_arr;   
  118.             return $page_arr
  119.         }     
  120.         //显示上一条,下一条     
  121.         function pagenav(){     
  122.             $this->set_url();     
  123.             $str = ''
  124.               
  125.             //$str .= $this->ipage.'/'.$this->sum_page; 
  126.               
  127.             for($i=1;$i<=$this->sum_page;$i++){ 
  128.                 if($i==$this->ipage) { 
  129.                     $str.= "<a href='#' class='cur'>".$i."</a> "
  130.                 }else
  131.                     $str.= "<a href='".$this->url.$i."'>".$i."</a> "
  132.                 } 
  133.             } 
  134.               
  135.                    
  136.             return $str;     
  137.         }     
  138.         function show_prv_next2(){     
  139.             $this->set_url();     
  140.             $str = ''
  141.               
  142.               
  143.               
  144.             if ($this->sum_page>1 and $this->ipage>1){     
  145.                 $str.= "<a href='".$this->url.($this->ipage-1)."'>上一页</a> ";     
  146.             }    
  147.             if ($this->sum_page>1 and $this->ipage<$this->sum_page){     
  148.                 $str .= "<a href='".$this->url.($this->ipage+1)."'>下一页</a>";     
  149.             }            
  150.             return $str;     
  151.         }     
  152.         function show_page_select(){     
  153.             if ($this->sum_page>1){     
  154.                 $str = "   <select onchange='location.href=this.options[this.selectedIndex].value'>";     
  155.                 for ($i=1; $i<=$this->sum_page; $i++){     
  156.                     $str.= "<option value='".$this->url.$i."' ".(($this->ipage)==$i ? " selected='selected'":"").">第".$i."页</option>";     
  157.                 }     
  158.                 $str.= "</select>";     
  159.             }     
  160.             return $str;     
  161.         }     
  162.         function show_page_select_wap(){     
  163.             if ($this->sum_page>1){     
  164.                 $str = "<select ivalue='".($this->ipage-1)."'>";     
  165.                 for ($i=1; $i<=$this->sum_page; $i++){     
  166.                     $str.= "<option onpick='".$this->url.$i."'>第".$i."节</option>";     
  167.                 }     
  168.                 $str.= "</select>";     
  169.             }     
  170.             return $str;     
  171.         }     
  172.         function set_url(){     
  173.             parse_str($_SERVER["QUERY_STRING"], $arr_url);     
  174.             unset($arr_url["ipage"]);     
  175.             if (emptyempty($arr_url)){     
  176.                 $str = "ipage=";     
  177.             }else{     
  178.                 $str = http_build_query($arr_url)."&ipage=";     
  179.             }     
  180.             $this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;     
  181.         }     
  182.     }     
  183. ?> 
  184.  

出处:http://www.phpfensi.com/php/20180531/10730.html


相关教程