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