• 技术文章 >后端开发 >php教程

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

    2016-07-15 13:32:08原创365

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

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

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


    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446155.htmlTechArticle今天我们就为大家写了一段PHP长文章分页函数,方便大家的代码编写: ?php functionff_page($content,$page) { global$expert_id; $ PageLength = 2000 ;//每页字...

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:方便快捷 PHP 文章 分页 函数 今天 我们 就为 大家 写了 一段 PHP 文章 分页 函数 方便
    上一篇:PHP跳转代码的实现方法讲解_PHP教程 下一篇:PHP分页类的具体使用方法_PHP教程
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 表单验证类 Validator for PHP_PHP• PHP文件上传的具体思路及实现_PHP• mysql数据库优化_PHP• 采用行写方式的聊天程序(之二)_PHP• [视频教程]LAMP兄弟连视PHP函数-printf
    1/1

    PHP中文网