Heim  >  Artikel  >  php教程  >  方便快捷的PHP长文章分页函数

方便快捷的PHP长文章分页函数

WBOY
WBOYOriginal
2016-06-13 11:09:27765Durchsuche

今天我们就为大家写了一段PHP长文章分页函数,方便大家的代码编写:

  1.  ?php   
  2. function ff_page($content,$page)   
  3. {   
  4. global $expert_id;   
  5. $PageLength = 2000; //每页字数   
  6. $CLength = strlen($content);   
  7. $PageCount = floor(($CLength 
    / $PageLength)) + 1; //计算页数   
  8. $PageArray=array();   
  9. $Seperator = array("n",
    "r","。","!","?",";
    ",",","”","’"); //分隔符号   
  10. //echo "页数:".$PageCount." br>";   
  11. //echo "长度:".$CLength." br> br> br>";   
  12. //strpos() 函数返回字符串在
    另一个字符串中第一次出现的位置   
  13. if($CLength$PageLength)   
  14. {   
  15. echo $content;   
  16. }else{   
  17. $PageArray[0]=0;   
  18. $Pos = 0;   
  19. $i=0;   
  20. //第一页   
  21. for($j=0;$j sizeof($Seperator);$j++)   
  22. {   
  23. //echo $Seperator[$j];   
  24. $Pos=strpos($content,
    $Seperator[$j],$PageArray[$i]+1900);   
  25. while($Pos > 0 && $Pos
     
     ($i+1)*$PageLength && 
    $Pos 
    > $i*$PageLength )   
  26. {   
  27. $PageArray[$i] = $Pos ;   
  28. $Pos = strpos($Pos+$PageLength,
    $content,$Seperator[$j]) ;   
  29. }   
  30. if($PageArray[$i]>0)   
  31. {   
  32. $j = $j + sizeof($Seperator) + 1;   
  33. }   
  34. }   
  35. //---   
  36. for($i=1;$i $PageCount-1;$i++){   
  37. for($j=0;$j sizeof($Seperator);$j++)   
  38. {   
  39. //echo $Seperator[$j];   
  40. $Pos=strpos($content,$Seperator
    [$j],$PageArray[$i-1]+1900);   
  41. while($Pos > 0 && $Pos  
    ($i+1)*$PageLength && $Pos 
    > 
    $i*$PageLength )   
  42. {   
  43. $PageArray[$i] = $Pos ;   
  44. $Pos = strpos($Pos+$PageLength,
    $content,$Seperator[$j]) ;   
  45. }   
  46. if($PageArray[$i]>0)   
  47. {   
  48. $j = $j + sizeof($Seperator) + 1;   
  49. }   
  50. }   
  51. }   
  52. //--PHP长文章分页函数最后一页   
  53. $PageArray[$PageCount-1]=$CLength;   
  54. //$page=2;   
  55. if($page==1)   
  56. {   
  57. $output=substr($content,0,
    $PageArray[$page-1]+2);   
  58. }   
  59. if($page>1 && $page =$PageCount)   
  60. {   
  61. $output=substr($content,$PageArray
    [$page-2]+2,$PageArray[$page-1]-$
    PageArray[$page-2]);   
  62. $output=" (上接第".($page-1)."页)n".$output;   
  63. }   
  64. echo str_replace("n"," br> 
      ",$output);   
  65. //if($page==$PageCount)   
  66. //return $output=substr($content,
    $PageArray[$page-2]+2,$PageArray[$page-1]-$PageArray[$page-2]);   
  67. if($PageCount>1)   
  68. {   
  69. echo " br> br> br> center>";   
  70. echo "font color='ff0000'>".$page." /font>/".$PageCount." 页  ";   
  71. if($page>1)   
  72. echo " a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page-1).">上一页 /a> ";   
  73. else   
  74. echo "上一页 ";   
  75. for($i=1;$i =$PageCount;$i++)   
  76. {   
  77. echo " a href=$PHP_SELF?expert_id=$expert_id&page_t=".$i.">[".$i."] /a> ";   
  78. }   
  79. if($page$PageCount)   
  80. echo "  a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page+1).">下一页 /a> ";   
  81. else   
  82. echo " 下一页 ";   
  83. echo " /center>";   
  84. }   
  85. }   
  86. }?>  

PHP长文章分页函数的使用
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li>
<span>$</span><span class="attribute">content1</span><span>=</span><span class="attribute-value">''</span><span>测试文字,<br>尽量长一些,www.corange.cn";   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">current</span><span>=$_REQUEST['page_t'];   </span>
</li>
<li>
<span>$</span><span class="attribute">result</span><span>=</span><span class="attribute-value">ff_page<br></span><span>($content1,$current);   </span>
</li>
<li class="alt"><span>echo $result;   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>

PHP长文章分页函数的使用与实现的代码示例如上所示。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn