长文章 自动/手动 分页类

如果您加了新功能,或者是有改进,请与大家一起分享。

长文章分页类,可以手工指定分页符,也可以让程序自动分页。

实例代码:请以UTF-8的文件编码进行测试。

  1. <?php
  2.     include('cutpage.php');
  3.     header("content-type:text/html;charset=utf-8");//设置页面编码
  4.     //自定义的长文章字符串,可以包含 html 代码,若字符串中有手动分页符 {nextpage} 则优先按手动分页符进行分页
  5.     $content = file_get_contents('text.txt');
  6.     $ipage = $_GET["ipage"]? intval($_GET["ipage"]):1;
  7.     $CP = new cutpage();
  8.     $CP->pagestr = $content;
  9.     $CP->cut_str();
  10.     echo $CP->pagearr[$ipage-1]."<hr/>";
  11.     echo $CP->show_prv_next();
  12. ?>

下载: cutpage.php
  1. <?php
  2. /*
  3. *    长文章 自动/手动 分页类
  4. *   @package    cutpage
  5. *   @author        yytcpt(无影)
  6. *   @version    2008-03-27
  7. *   @copyrigth    http://www.d5s.cn/ 
  8. */
  9.     class cutpage{
  10.         var $pagestr;        //被切分的内容
  11.         var $pagearr;        //被切分文字的数组格式
  12.         var $sum_word;        //总字数(UTF-8格式的中文字符也包括)
  13.         var $sum_page;        //总页数
  14.         var $page_word;        //一页多少字
  15.         var $cut_tag;        //自动分页符
  16.         var $cut_custom;    //手动分页符
  17.         var $ipage;            //当前切分的页数,第几页
  18.         var $url;
  19.         function __construct(){
  20.             $this->page_word = 1000;
  21.             $this->cut_tag = array("</table>", "</div>", "</p>", "<br/>", "”。", "", ".", "", "……", "", ",");
  22.             $this->cut_custom = "{nextpage}";
  23.             $tmp_page = intval(trim($_GET["ipage"]));
  24.             $this->ipage = $tmp_page>1?$tmp_page:1;
  25.         }
  26.         //统计总字数
  27.         function get_page_word(){
  28.             $this->sum_word = $this->strlen_utf8($this->pagestr);
  29.             return $this->sum_word;
  30.         }
  31.         /*    统计UTF-8编码的字符长度
  32.          *    一个中文,一个英文都为一个字
  33.          */
  34.         function strlen_utf8($str){
  35.            $i = 0;
  36.            $count = 0;
  37.            $len = strlen ($str);
  38.            while ($i < $len){
  39.                $chr = ord ($str[$i]);
  40.                $count++;
  41.                $i++;
  42.                if ($i >= $len)
  43.                    break;
  44.                if ($chr & 0x80){
  45.                    $chr <<= 1;
  46.                    while ($chr & 0x80) {
  47.                        $i++;
  48.                        $chr <<= 1;
  49.                    }
  50.                }
  51.            }
  52.            return $count;
  53.         }
  54.         //设置自动分页符号
  55.         function set_cut_tag($tag_arr=array()){
  56.             $this->cut_tag = $tag_arr;
  57.         }
  58.         //设置手动分页符
  59.         function set_cut_custom($cut_str){
  60.             $this->cut_custom = $cut_str;
  61.         }
  62.         function show_cpage($ipage=0){
  63.             $this->cut_str();
  64.             $ipage = $ipage ? $ipage:$this->ipage;
  65.             return $this->pagearr[$ipage];
  66.         }
  67.         function cut_str(){
  68.             $str_len_word = strlen($this->pagestr);        //获取使用strlen得到的字符总数
  69.             $i = 0;
  70.             if ($str_len_word<=$this->page_word){    //如果总字数小于一页显示字数
  71.                 $page_arr[$i] = $this->pagestr;
  72.             }else{
  73.                 if (strpos($this->pagestr, $this->cut_custom)){
  74.                     $page_arr = explode($this->cut_custom, $this->pagestr);
  75.                 }else{
  76.                     $str_first = substr($this->pagestr, 0, $this->page_word);    //0-page_word个文字    cutStr为func.global中的函数
  77.                     foreach ($this->cut_tag as $v){
  78.                         $cut_start = strrpos($str_first, $v);        //逆向查找第一个分页符的位置
  79.                         if ($cut_start){
  80.                             $page_arr[$i++] = substr($this->pagestr, 0, $cut_start).$v;
  81.                             $cut_start = $cut_start + strlen($v);
  82.                             break;
  83.                         }
  84.                     }
  85.                     if (($cut_start+$this->page_word)>=$str_len_word){    //如果超过总字数
  86.                         $page_arr[$i++] = substr($this->pagestr, $cut_start, $this->page_word);
  87.                     }else{
  88.                         while (($cut_start+$this->page_word)<$str_len_word){
  89.                             foreach ($this->cut_tag as $v){
  90.                                 $str_tmp = substr($this->pagestr, $cut_start, $this->page_word);        //取第cut_start个字后的page_word个字符
  91.                                 $cut_tmp = strrpos($str_tmp, $v);        //找出从第cut_start个字之后,page_word个字之间,逆向查找第一个分页符的位置
  92.                                 if ($cut_tmp){
  93.                                     $page_arr[$i++] = substr($str_tmp, 0, $cut_tmp).$v;
  94.                                     $cut_start = $cut_start + $cut_tmp + strlen($v);
  95.                                     break;
  96.                                 }
  97.                             }   
  98.                         }
  99.                         if (($cut_start+$this->page_word)>$str_len_word){
  100.                             $page_arr[$i++] = substr($this->pagestr, $cut_start, $this->page_word);
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.             $this->sum_page = count($page_arr);        //总页数
  106.             $this->pagearr = $page_arr;
  107.         }
  108.         //显示上一条,下一条
  109.         function show_prv_next(){
  110.             $this->set_url();
  111.             if ($this->sum_page>1 and $this->ipage<$this->sum_page){
  112.                 $str = "<a href='".$this->url.($this->ipage+1)."'>下一页</a> ";
  113.             }
  114.             if ($this->sum_page>1 and $this->ipage>1){
  115.                 $str.= "<a href='".$this->url.($this->ipage-1)."'>上一页</a>";
  116.             }
  117.             return $str;
  118.         }
  119.         function show_page_select(){
  120.             if ($this->sum_page>1){
  121.                 $str = " &nbsp; <select onchange=\"location.href=this.options[this.selectedIndex].value\">";
  122.                 for ($i=1; $i<=$this->sum_page; $i++){
  123.                     $str.= "<option value='".$this->url.$i."' ".(($this->ipage)==$i ? " selected='selected'":"").">第".$i."页</option>";
  124.                 }
  125.                 $str.= "</select>";
  126.             }
  127.             return $str;
  128.         }
  129.         function show_page_select_wap(){
  130.             if ($this->sum_page>1){
  131.                 $str = "<select ivalue='".($this->ipage-1)."'>";
  132.                 for ($i=1; $i<=$this->sum_page; $i++){
  133.                     $str.= "<option onpick='".$this->url.$i."'>第".$i."节</option>";
  134.                 }
  135.                 $str.= "</select>";
  136.             }
  137.             return $str;
  138.         }
  139.         function set_url(){
  140.             parse_str($_SERVER["QUERY_STRING"], $arr_url);
  141.             unset($arr_url["ipage"]);
  142.             if (empty($arr_url)){
  143.                 $str = "ipage=";
  144.             }else{
  145.                 $str = http_build_query($arr_url)."&ipage=";
  146.             }
  147.             $this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;
  148.         }
  149.     }
  150. ?>
此条目发表在 程序 分类目录。将固定链接加入收藏夹。

长文章 自动/手动 分页类》有 1 条评论

  1. 黑鸡特功队 说:

    大哥,请问如果要手动分页,那么我应该在文本里加些什么?是不是加{nextpage}这个,但是我不行啊,请教教我

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>