方便快捷的PHP长文章分页函数
创始人
2024-06-13 17:40:49
0

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

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

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

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

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...