当前位置:Gxlcms > PHP教程 > php文本文章分页代码示例

php文本文章分页代码示例

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

  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. 测试文章分页

人气教程排行