php对内容较长的文章进行分页
发布:smiling 来源: PHP粉丝网 添加日期:2018-05-31 10:52:47 浏览: 评论:
- require "page.class.php";
- $neirong = "内容内容内容内容内容内容内容内容内容内容内容内容内容内容";
- $content = file_get_contents($neirong);
- $ipage = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;
- $CP = new cutpage($content);
- $page = $CP->cut_str();
- echo $page[$ipage - 1];
- echo '' . $CP->pagenav() . '';
-
-
-
-
-
- <?php
-
-
-
- class cutpage{
- private $pagestr;
- private $pagearr;
- private $sum_word;
- 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;
- }
-
-
-
- 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);
- $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);
- 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_tmp = strrpos($str_tmp, $v);
- 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 = '';
-
-
-
- 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 (emptyempty($arr_url)){
- $str = "ipage=";
- }else{
- $str = http_build_query($arr_url)."&ipage=";
- }
- $this->url = "http://".$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"]."?".$str;
- }
- }
- ?>
分享到:
相关文章
- ·php中分页显示文章标题(2013-12-10)
- ·php长文章分页原理与实现程序(2014-01-05)
- ·php文章内容分页实例程序(2014-01-07)
- ·php 文章内容分页代码(2014-09-09)
- ·深入分析PHP文章内容分页(2015-04-11)
- ·PHP长文章分页的示例(2016-08-25)
- ·PHP长文章分页的使用例子(2016-08-30)