PHP头条
热点:

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


 PHP程序员们在编写程序代码的时候,通常都会用到文章的分页等功能的实现。每次编写不同的程序都需要编写一遍文章分页的代码,比较玛法。

  • 推荐几种PHP实现页面跳转的方法
  • 为你详解PHP页面跳转函数的具体含义
  • PHP header函数的详细用法介绍
  • 经验分享 总结PHP常用函数
  • 一劳永逸 打造简单PHP分页函数

今天我们就为大家写了一段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>&nbsp;
    &nbsp;&nbsp;",$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." 页 &nbsp;";   
  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长文章分页函数的使用
 

  1. < ?php   
  2. $content1=''测试文字,
    尽量长一些,www.corange.cn";   
  3. $current=$_REQUEST['page_t'];   
  4. $result=ff_page
    ($content1,$current);   
  5. echo $result;   
  6. ?>  

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

www.phpzy.comtrue/php/13317.htmlTechArticle方便快捷的PHP长文章分页函数 PHP程序员们在编写程序代码的时候,通常都会用到文章的分页等功能的实现。每次编写不同的程序都需要编写一遍文章分页的代码,比较玛法。 推荐几种...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐