PHP长文章分页的使用例子

文章分页用到比较多同时使用过cms的朋友也碰到过都具备了文章分页功能了,下面我们自己来看一个关于文章分页的例子,具体的操作步骤如下所示,希望文章能够帮助到各位朋友.

content.txt:这个文件里面放的是内容了,如果你是与mysql数据库结合的话只要把把这一句换成你的数据库的内容即可.

index.php文件:

  1. <link rel="stylesheet" type="text/css" href="http:///jquery/css/common.css" />
  2. <style type="text/css">
  3. .demo{width:80%;margin:50px auto 10px auto;padding:10px;}
  4. .demo p{line-height:30px;text-indent:2em}
  5. .demo h3{font-size:24px;text-align:center;padding:10px}
  6. @media (max-width: 480px){
  7. .demo{width:360px;margin:50px auto 10px auto;padding:10px;}
  8. .demo img{width:90%}
  9. .demo h3{font-size:1.5em;line-height:1.9em}
  10. }
  11. .pages{text-align:center;border-top:1px solid #d3d3d3;margin-top:10px; padding-top:10px}
  12. .pages a{display:inline-block;margin:4px;padding:4px 8px;background:#f9f9f9;border:1px solid #d9d9d9}
  13. .pages a.cur{cursor:default;background:#d3d3d3;color:#434343}
  14. .pages a.cur:hover{text-decoration:none}
  15. </style>
  16. <div class="head">
  17. <div class="head_inner clearfix">
  18. <ul >
  19. <li><a href="http://">首 页</a></li>
  20. <li><a href="http:///templates">网站模板</a></li>
  21. <li><a href="http:///js">网页特效</a></li>
  22. <li><a href="http:///php">PHP</a></li>
  23. <li><a href="http:///site">精选网址</a></li>
  24. </ul>
  25. <a class="logo" href="http://"><img src="http:///Public/images/logo.jpg" alt="素材火logo" /></a>
  26. </div>
  27. </div>
  28. <div class="container">
  29. <div class="demo">
  30. <h2 class="title"><a href="http:///js/639.html">教程:PHP长文章分页教程与演示</a></h2>
  31. <h3>未来10年千万人将没有工作?</h3>
  32. <?php
  33. include('page.class.php');
  34. //自定义的长文章字符串,可以包含 html 代码,若字符串中有手动分页符 {nextpage} 则优先按手动分页符进行分页
  35. $content = file_get_contents('content.txt');
  36. $ipage = isset($_GET["ipage"]) ? intval($_GET["ipage"]) : 1;
  37. $CP = new cutpage($content);
  38. $page = $CP->cut_str();
  39. echo $page[$ipage - 1];
  40. echo '<div class="pages">' . $CP->pagenav() . '</div>';
  41. ?>
  42. </div>
  43. </div>

长文章分页类 page.class.php:

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