当前位置:Gxlcms > PHP教程 > php文本分页代码:php读取txt文本文件并分页显示

php文本分页代码:php读取txt文本文件并分页显示

时间:2021-07-01 10:21:17 帮助过:27人阅读

  1. session_start();
  2. if (empty($page)) {$page=1;}
  3. if (isset($_GET['page'])==TRUE) {$page=$_GET['page']; }
  4. ?>
  5. Read Result
  6. if($page){
  7. $counter=file_get_contents("example.txt"); //读取txt文件内容到$counter
  8. $length=strlen($counter);
  9. $page_count=ceil($length/5000);
  10. function msubstr($str,$start,$len){
  11. $strlength=$start+$len;
  12. $tmpstr="";
  13. for($i=0;$i<$strlength;$i++) {
  14. if(ord(substr($str,$i,1))==0x0a) {
  15. $tmpstr.='
    ';
  16. }
  17. if(ord(substr($str,$i,1))>0xa0) {
  18. $tmpstr.=substr($str,$i,2);
  19. $i++;
  20. }
  21. else{
  22. $tmpstr.=substr($str,$i,1); }
  23. }
  24. return $tmpstr;
  25. }
  26. //------------截取中文字符串---------
  27. $c=msubstr($counter,0,($page-1)*5000);
  28. $c1=msubstr($counter,0,$page*5000);
  29. echo substr($c1,strlen($c),strlen($c1)-strlen($c));
  30. }?>
  31. /
  32. echo "首页 ";
  33. if($page!=1){
  34. echo "上一页 ";
  35. }
  36. if($page<$page_count){
  37. echo "下一页 ";
  38. }
  39. echo "尾页";
  40. ?>

二、php读取文件内容

例子:

  1. /*
  2. 作者:bjf;
  3. 应用:读取文件内容;
  4. */
  5. function read_file_content($FileName)
  6. {
  7. //open file
  8. $fp=fopen($FileName,"r");
  9. $data="";
  10. while(!feof($fp))
  11. {
  12. //read the file
  13. $data.=fread($fp,4096);
  14. }
  15. //close the file
  16. fclose($fp);
  17. //delete the file
  18. //unlink($FileName);
  19. //return the content from the file
  20. echo $data;
  21. }
  22. read_file_content("a.html")
  23. ?>

fread与fgets的区别 fread :以字节位计算长度,按照指定的长度和次数读取数据,遇到结尾或完成指定长度读取后停止. fgets :整行读取,遇到回车换行或结尾停止.在文本方式时使用.

三、php长文章分页显示

例子:

  1. /**
  2. *Author:乌鸟heart
  3. *长文章分页的代码
  4. *原理:
  5. *利用一个数组来记录文章每一页(用p0、p1、p2...做手动标记)的起始字节数,然后通过利用php函数操作这个数组去显示分页后的文章。分页显示,传递ptag(与tag的值一样)值。
  6. *利用到的php函数:
  7. *1、strlen("字符串") - Returns the length of the given string. - 返回字符串的字节总数。
  8. *2、strpos("字符串","匹配字符") - Returns the numeric position of the first occurrence of needle in the haystack string. - 返回字符串中出现的第一个相匹配的字符所在的字节序数。
  9. *3、substr("字符串","起始位置","终止位置") - substr() returns the portion of string specified by the start and length parameters. - 返回字符串中指定起止位置的若干字符。
  10. */
  11. $sql = "select * from article where id = 41";//定义sql语句,返回id为41的内容
  12. $result = mysql_query($sql);//执行sql语句,返回结果集
  13. $row = mysql_fetch_array($result);//以数组的形式从记录集返回
  14. $content = $row['content'];//把文章赋给变量$content
  15. $articleCounts = strlen($content);//返回$content(文章)的总字节数
  16. $isTrue = true;//循环标记
  17. $tag = 0;//分页标记、数组下标
  18. echo "字节总数:".$articleCounts."
    ";//测试信息
  19. //寻找标记“ptag”,并把其位置(所在的字节数)赋给数组array[]------------------------------------------
  20. while($isTrue){
  21. $startAt = strpos($content,"p".$tag);//得到相应ptag的字节序数
  22. if($startAt != false){ //如果有标记(返回值不是false),则记录位置
  23. $array[$tag++] = $startAt;
  24. }else{ //如果没有标记,则将数组array[0]赋值'\0'
  25. $array[$tag] = '\0';
  26. $isTrue = false;
  27. }
  28. }
  29. //循环输出标记位置--------测试信息
  30. for($i = 0; $i < $tag; $i++){
  31. echo $array[$i]."
    ";
  32. }
  33. echo "------------------------------
    ";
  34. //输出内容----
  35. if($array[0] == '\0'){ //判断是否有标记
  36. echo $content; //没有标记的情况,单页显示
  37. }else{ //有标记的情况,分页显示
  38. //输出分页内容
  39. if( isset($_GET['ptag']) ){ //判断是否有ptag值传递,有则显示第 ptag+1 页,否则显示第一页(ptag=0)
  40. $ptag = $_GET['ptag']; //把ptag的值赋给变量$ptag
  41. if($ptag < $tag){ //判断参数是否有误
  42. echo "有值传递,显示第".($ptag+1)."页
    "; //测试信息
  43. echo "值为:".$ptag."
    "; //测试信息
  44. echo substr($content,$array[$ptag - 1] + 2,$array[$ptag] - $array[$ptag - 1] - 2);//显示ptag+1页的内容
  45. }else{echo "参数有误";}
  46. }
  47. else{ //没有ptag值传递的情况,显示第一页(ptag=0)
  48. echo "无值传递,显示第1页
    "; //测试信息
  49. echo substr($content,0,$array[0] - 1);//显示第一页的内容
  50. }
  51. }
  52. //循环显示页数链接----
  53. if($array[0] != '\0'){ //在有手动标记的情况下才显示页数链接
  54. for($i = 0;$i < $tag;$i++){
  55. if($ptag == $i){ //如果是本页,则粗体显示
  56. $pager .= " ".($i+1)." ";
  57. }else{ //不是本页
  58. $pager .= " ".($i+1)." ";
  59. }
  60. }
  61. echo "
    跳转至第".$pager."页"; //输出链接
  62. }
  63. ?>

人气教程排行