您的位置:澳门402永利com > 澳门402永利com网络 > PHP长作品分页的应用例子,KindEditor小说内分页的

PHP长作品分页的应用例子,KindEditor小说内分页的

发布时间:2019-09-27 06:21编辑:澳门402永利com网络浏览(51)

    自个儿的敞亮大旨代码

    content.txt

     代码如下

    其一文件之中放的是内容了,尽管你是与mysql数据库组成的话只要把把这一句换到你的数据库的内容就能够。

    $news = array(
    'title'=>'first',
    'content' =>'<p>this is paragraph 1</p>'
     .'<p>this is paragraph 2</p>'
     .'Now i make a page end. [--page--] page 2 start~'
    );

    index.php文件

    $newsModel->insert($news);

    //news_detail.php?p=2 :

    $news_one = $newsModel->get(1);

    $content_pages = explode('[--page--]',$news_one['content']);

    $cur_page = min( max(1,intval($_GET['p'])), count($content_pages) );

    $news_one['content'] = $content_pages[$cur_page-1];

    return render('news_detail.html', $news_one);

    大家知道在众多在线编辑器里都有插入分页的职能,像在KindEditor中式茶食击插入分页编辑器会在光标的地点插入如下的代码:

     代码如下

    <hr style="page-break-after:always;" class="ke-pagebreak" />

    而U艾德itor则会在光标处步向:

    _ueditor_page _break_tag__ueditor_page和_break_tag_中间是从未有过空格的,由于本网站也是用的ueditor使用的分页符是break_tag,所以地点加了空格防止影响

    上面笔者以KindEditor为例,贴上自己的代码:

     代码如下

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    <?php
    $content=<<<str
            <p>法国巴黎顶想新闻科学技术有限公司(TOPThink Inc.)是境内抢先的WEB应用和服务提供商致力于WEB应用平台、产品和平运动用的研究开发和服务,为企工作单位提供基于WEB的运用开荒飞快实施方案和制品。公司建设构造于二〇一〇年十一月,是一家怀有自己作主知识产权的高新工夫公司。</p>
            <hr />
            <p>集司令员时间潜心于WEB应用框架、应用平台和同盟社排纷解难方案的钻研,公司的宗旨手艺框架ThinkPHP由创办人刘晨于二零零六年创建,经过6年多的细致制作和进步,具备普及的客户基础和完美的正统口碑,已经成长为本国超越和最具影响力的WEB应用开采框架,海外同期相比较也享有一定大的优势。其应用领域遍及于各种行当,在门户、社区和电子商务领域有着特别卓越扶助以及进行,大小案例不下千家,在安全、作用、负载上皆有非常大优势,已经产生WEB应用的迅猛支付技术方案和极品实施!</p>
                    <hr />
            <p>
            集团分局位于北京,由从事网络和客户体验商讨达10年的闻名专家领军,具有一群职业的谋算、设计和本事共青团和少先队以及布满的社区才技艺量。公市长久以来依赖专门的学问的影响力、卓绝的顾客和搭档关系,特邀了累累康宁和类型专家作为顾问,有力得保障了客商项指标开销和实践。企业还会有所一支资深顾客体验和布置性切磋队容,针对区别客户量身定做顾客体验流程,有着不错的产品设计和绸缪概念。<br/>二〇一〇年到手Hong Kong市最主要文艺术创作作项目选题推荐。</p>
    str;

    $content = preg_replace("'<hr(.*)/>'iUs", "<hr>", $content);
    $content = explode("<hr>", $content);
    $count = count($content);
    $p = empty($_GET['page']) ? 1 : (int) $_GET[page];
    $p = $p == 0 ? 1 : $p;
    $p = $p > $count ? $count : $p;
    $p--;
    $content = $content[$p];
    $p++;
    if ($count > 1) {
        $page = '<a class="first" href="?page=1">首页</a>';
        for ($i = 1; $i <= $count; $i++) {
            $page.=$i == $p ? '【<a class="on" style="color:red;" href="javascript:void(0)">' . $i . '</a>】' : '【<a href="?page=' . $i . '">' . $i . '</a>】';
        }
        $page.='<a class="end" href="?page=' . $count . '">尾页</a>';

    }
    echo "<h1>香港(Hong Kong)顶想音讯科技(science and technology)有限公司</h1>";
    echo $content;

    echo "<div>{$page}</div>";

    ?>
    </body>
    </html>

    安份守己上边的代码你就足以兑现小说分页了,效果如下图:

    图片 1jquery1900003197585295995675="146" onappear="null" loaded="true" original="/Uploads/Article/2013-09-25/20130925231340.png" alt="" />

    即使你用的是UEditor编辑器分页符暗中认可是:_ueditor_page _break_tag_ (你能够在ueditor.config.js中期维修改pageBreakTag的设定)则能够将地点的代码稍作修改就可以:

     代码如下

    $content = preg_replace("'<hr(.*)/>'iUs", "<hr>", $content);
    $content = explode("<hr>", $content);

    改为:

    $content = explode("_ueditor_page _break_tag_", $content);

    如此那般就足以了,当然你能够自定义任何分页符

          
    <link rel="stylesheet" type="text/css" href="" />
            <style type="text/css">
                .demo{width:80%;margin:50px auto 10px auto;padding:10px;}
                .demo p{line-height:30px;text-indent:2em}
                .demo h3{font-size:24px;text-align:center;padding:10px}
                @media (max-width: 480px){
                    .demo{width:360px;margin:50px auto 10px auto;padding:10px;}
                    .demo img{width:90%}
                    .demo h3{font-size:1.5em;line-height:1.9em}
                }
                .pages{text-align:center;border-top:1px solid #d3d3d3;margin-top:10px; padding-top:10px}
                .pages a{display:inline-block;margin:4px;padding:4px 8px;background:#f9f9f9;border:1px solid #d9d9d9}
                .pages a.cur{cursor:default;background:#d3d3d3;color:#434343}
                .pages a.cur:hover{text-decoration:none}
            </style>

            <div class="head">
                <div class="head_inner clearfix">
                    <ul id="nav">
                        <li><a href=" 页</a></li>
                        <li><a href=";
                        <li><a href=";
                        <li><a href=";
                        <li><a href=";
                    </ul>
                    <a class="logo" href=" src="" alt="素材火logo" /></a>
                </div>
            </div>
            <div class="container">
                <div class="demo">
                    <h2 class="title"><a href=";
                    <h3>以后10年千万人将尚未工作?</h3>
                    <?php
                    include('page.class.php');
                    //自定义的长文章字符串,能够满含 html 代码,若字符串中有手动分页符 {nextpage} 则优先按手动分页符实行分页   
                    $content = file_get_contents('content.txt');
                    $ipage = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;
                    $CP = new cutpage($content);
                    $page = $CP->cut_str();
                    echo $page[$ipage - 1];
                    echo '<div class="pages">' . $CP->pagenav() . '</div>';
                    ?>

                </div>
            </div>

    长小说分页类  page.class.php

    <?php   
    /* 
    *  长作品分页类    
    */ 
        class cutpage{   
            private $pagestr;       //被切分的源委   
            private $pagearr;       //被切分文字的数组格式   
            private $sum_word;      //总篇幅(UTF-8格式的华语字符也囊括)   
            private $sum_page;      //总页数   
            private $page_word;     //一页多少字   
            private $cut_tag;       //自动分页符   
            private $cut_custom;    //手动分页符   
            private $ipage;         //当前切分的页数,第几页   
            private $url;   
      
            function __construct($pagestr,$page_word=1000){   
                $this->page_word = $page_word;   
                $this->cut_tag = array("</table>", "</div>", "</p>", "<br/>", "”。", "。", ".", "!", "……", "?", ",");   
                $this->cut_custom = "{nextpage}";   
                $tmp_page = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;   
                $this->ipage = $tmp_page>1?$tmp_page:1;
       $this->pagestr = $pagestr;
            }   
            //总计总字数   
            function get_page_word(){   
                $this->sum_word = $this->strlen_utf8($this->pagestr);   
                return $this->sum_word;   
            }   
            /*  总括UTF-8编码的字符长度 
             *  叁个华语,一个立陶宛(Lithuania)语都为一个字 
             */ 
            function strlen_utf8($str){   
               $i = 0;   
               $count = 0;   
               $len = strlen ($str);   
               while ($i < $len){   
                   $chr = ord ($str[$i]);   
                   $count++;   
                   $i++;   
                   if ($i >= $len)   
                       break;   
                   if ($chr & 0x80){   
                       $chr <<= 1;   
                       while ($chr & 0x80) {   
                           $i++;   
                           $chr <<= 1;   
                       }   
                   }   
               }   
               return $count;   
            }   
            //设置自动分页符号   
            function set_cut_tag($tag_arr=array()){   
                $this->cut_tag = $tag_arr;   
            }   
            //设置手动分页符   
            function set_cut_custom($cut_str){   
                $this->cut_custom = $cut_str;   
            }   
            function show_cpage($ipage=0){   
                $this->cut_str();   
                $ipage = $ipage ? $ipage:$this->ipage;   
                return $this->pagearr[$ipage];   
            }   
            function cut_str(){   
                $str_len_word = strlen($this->pagestr);     //获取使用strlen获得的字符总量   
                $i = 0;   
                if ($str_len_word<=$this->page_word){   //假如总字数小于一页显示字数   
                    $page_arr[$i] = $this->pagestr;   
                }else{   
                    if (strpos($this->pagestr, $this->cut_custom)){   
                        $page_arr = explode($this->cut_custom, $this->pagestr);   
                    }else{   
                        $str_first = substr($this->pagestr, 0, $this->page_word);   //0-page_word个文字    cutStr为func.global中的函数   
                        foreach ($this->cut_tag as $v){   
                            $cut_start = strrpos($str_first, $v);       //逆向找出第三个分页符的岗位   
                            if ($cut_start){   
                                $page_arr[$i++] = substr($this->pagestr, 0, $cut_start).$v;   
                                $cut_start = $cut_start + strlen($v);   
                                break;   
                            }   
                        }   
                        if (($cut_start+$this->page_word)>=$str_len_word){  //假使超过总字数   
                            $page_arr[$i++] = substr($this->pagestr, $cut_start, $this->page_word);   
                        }else{   
                            while (($cut_start+$this->page_word)<$str_len_word){   
                                foreach ($this->cut_tag as $v){   
                                    $str_tmp = substr($this->pagestr, $cut_start, $this->page_word);        //取第cut_start个字后的page_word个字符   
                                    $cut_tmp = strrpos($str_tmp, $v);       //寻觅从第cut_start个字之后,page_word个字里面,逆向查找第七个分页符的岗位   
                                    if ($cut_tmp){   
                                        $page_arr[$i++] = substr($str_tmp, 0, $cut_tmp).$v;   
                                        $cut_start = $cut_start + $cut_tmp + strlen($v);   
                                        break;   
                                    }   
                                }     
                            }   
                            if (($cut_start+$this->page_word)>$str_len_word){   
                                $page_arr[$i++] = substr($this->pagestr, $cut_start, $this->page_word);   
                            }   
                        }   
                    }   
                }   
                $this->sum_page = count($page_arr);     //总页数   
                $this->pagearr = $page_arr; 
       return $page_arr;
            }   
            //彰显上一条,下一条   
            function pagenav(){   
                $this->set_url();   
                $str = '';
       
       //$str .= $this->ipage.'/'.$this->sum_page;
       
       for($i=1;$i<=$this->sum_page;$i++){
        if($i==$this->ipage) {
         $str.= "<a href='#' class='cur'>".$i."</a> ";
        }else{
         $str.= "<a href='".$this->url.$i."'>".$i."</a> ";
        }
       }
       
                 
                return $str;   
            }   
      function show_prv_next2(){   
                $this->set_url();   
                $str = '';
       
       
       
                if ($this->sum_page>1 and $this->ipage>1){   
                    $str.= "<a href='".$this->url.($this->ipage-1)."'>上一页</a> ";   
                }  
       if ($this->sum_page>1 and $this->ipage<$this->sum_page){   
                    $str .= "<a href='".$this->url.($this->ipage+1)."'>下一页</a>";   
                }      
                return $str;   
            }   
            function show_page_select(){   
                if ($this->sum_page>1){   
                    $str = "   <select onchange='location.href=this.options[this.selectedIndex].value'>";   
                    for ($i=1; $i<=$this->sum_page; $i++){   
                        $str.= "<option value='".$this->url.$i."' ".(($this->ipage)==$i ? " selected='selected'":"").">第".$i."页</option>";   
                    }   
                    $str.= "</select>";   
                }   
                return $str;   
            }   
            function show_page_select_wap(){   
                if ($this->sum_page>1){   
                    $str = "<select ivalue='".($this->ipage-1)."'>";   
                    for ($i=1; $i<=$this->sum_page; $i++){   
                        $str.= "<option onpick='".$this->url.$i."'>第".$i."节</option>";   
                    }   
                    $str.= "</select>";   
                }   
                return $str;   
            }   
            function set_url(){   
                parse_str($_SERVER["QUERY_STRING"], $arr_url);   
                unset($arr_url["ipage"]);   
                if (empty($arr_url)){   
                    $str = "ipage=";   
                }else{   
                    $str = http_build_query($arr_url)."&ipage=";   
                }   
                $this->url = ";   
            }   
        }   
    ?>

    本文由澳门402永利com发布于澳门402永利com网络,转载请注明出处:PHP长作品分页的应用例子,KindEditor小说内分页的

    关键词: