php文本文章分頁程式碼範例

來源:互聯網
上載者:User
  1. /**
  2. * **********************************************************
  3. * Read Me
  4. * 文章分頁
  5. *
  6. * 分頁方式,可以按字數分頁,按換行分頁,按特殊標記分頁等
  7. * 其實實現思路是一樣的,只是將其按一定規律放入一個數組
  8. * 然後根據 url 傳入的參數取得某個片段即可
  9. *
  10. *
  11. * filename: page.php
  12. * charset: UTF-8
  13. * create date: 2012-5-16
  14. * **********************************************************
  15. * @author itbdw
  16. * @copyright (C) 2011-2012 itbdw
  17. * @link http://weibo.com/itbudaoweng
  18. * @url http://bbs.it-home.org
  19. */
  20. header('Content-Type:text/html; charset=utf-8');
  21. ?>
  22. $title = 'Pagination Test';
  23. //需要分頁的資料
  24. $data = <<Hey, guys. I am here to test if it is working.
  25. This pagination is very simple, isn't it?
  26. And I tried to use different method to page it.
  27. Can you see it?
  28. DATA;
  29. //當前文章頁
  30. $page = 0;
  31. //初始文章長度
  32. $length = 0;
  33. //分頁長度
  34. $perpage = 160;
  35. //顯示在頁面的代碼
  36. $link = '';
  37. //分割後的數組
  38. $strArr = array();
  39. $page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  40. $length = strlen($data);
  41. //按字數分割
  42. // $str = str_split($data, $perpage);
  43. //按字元分割
  44. $delimiter = "\n";
  45. // $delimiter = '<--pagination-->';
  46. $strArr = explode($delimiter, $data);
  47. $strNum = count($strArr);
  48. $content = $strArr[$page];
  49. if ($strNum > 1) {
  50. if ($page != 0) {
  51. $link .= '首頁';
  52. } else {
  53. $link .= '首頁';
  54. }
  55. for ($n = 0; $n < $strNum; $n++) {
  56. if ($n == $page) {
  57. $link .= '' . ($n + 1) . '';
  58. } else {
  59. $link .= "" . ($n + 1) . "";
  60. }
  61. }
  62. $link .= '';
  63. if ($page != ($strNum - 1)) {
  64. $link .= "尾頁";
  65. } else {
  66. $link .= '尾頁';
  67. }
  68. }
  69. ?>
  70. 測試文章分頁
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.