php文本分頁代碼:php讀取txt文字檔並分頁顯示

來源:互聯網
上載者:User
  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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.