PHP 文本文章分页代码 按标记或长度(不涉及数据库)
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
实例代码:
复制代码 代码如下:
<?php
/**
* **********************************************************
* Read Me
* 文章分页
*
* 分页方式,可以按字数分页,按换行分页,按特殊标记分页等
* 其实实现思路是一样的,只是将其按一定规律放入一个数组
* 然后根据 url 传入的参数取得某个片段即可
* 大家完全可以写一个功能强大的函数保存起来以备不时之需
*
* 题外话:很多编辑器都有插入分页按钮,利用插入的代码可显示分页
*
* filename: page.php
* charset: UTF-8
* create date: 2012-5-16
* **********************************************************
* @author itbdw <itbudaoweng@gmail.com>
* @copyright (C) 2011-2012 itbdw
* @link http://weibo.com/itbudaoweng
*/
header('Content-Type:text/html; charset=utf-8');
?>
<?php
$title = 'Pagination Test';
//需要分页的数据
$data = <<<DATA
Hey, guys. I am here to test if it is working.
This pagination is very simple, isn't it?<!--pagination-->
And I tried to use different method to page it.
Can you see it?
DATA;
//当前文章页
$page = 0;
//初始文章长度
$length = 0;
//分页长度
$perpage = 160;
//显示在页面的代码
$link = '';
//分割后的数组
$strArr = array();
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$length = strlen($data);
//按字数分割
// $str = str_split($data, $perpage);
//按字符分割
$delimiter = "\n";
// $delimiter = '<--pagination-->';
$strArr = explode($delimiter, $data);
$strNum = count($strArr);
$content = $strArr[$page];
if ($strNum > 1) {
if ($page != 0) {
$link .= '<a href="?page=0">首页</a>';
} else {
$link .= '<span>首页</span>';
}
for ($n = 0; $n < $strNum; $n++) {
if ($n == $page) {
$link .= '<span>' . ($n + 1) . '</span>';
} else {
$link .= "<a href='?page={$n}'>" . ($n + 1) . "</a>";
}
}
$link .= '';
if ($page != ($strNum - 1)) {
$link .= "<a href='?page=" . ($strNum - 1) . "'>尾页</a>";
} else {
$link .= '<span>尾页</span>';
}
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
font-family: '微软雅黑';
}
.link a, span {
margin: 1px;
padding: 1px;
}
.link span {
color: #777;
}
.link a {
color: #26A2DA;
text-decoration: none;
}
</style>
<title>测试文章分页</title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>
<hr />
<p class="link"><?php echo $link; ?></p>
</body>
</html>
复制代码 代码如下:
<?php
/**
* **********************************************************
* Read Me
* 文章分页
*
* 分页方式,可以按字数分页,按换行分页,按特殊标记分页等
* 其实实现思路是一样的,只是将其按一定规律放入一个数组
* 然后根据 url 传入的参数取得某个片段即可
* 大家完全可以写一个功能强大的函数保存起来以备不时之需
*
* 题外话:很多编辑器都有插入分页按钮,利用插入的代码可显示分页
*
* filename: page.php
* charset: UTF-8
* create date: 2012-5-16
* **********************************************************
* @author itbdw <itbudaoweng@gmail.com>
* @copyright (C) 2011-2012 itbdw
* @link http://weibo.com/itbudaoweng
*/
header('Content-Type:text/html; charset=utf-8');
?>
<?php
$title = 'Pagination Test';
//需要分页的数据
$data = <<<DATA
Hey, guys. I am here to test if it is working.
This pagination is very simple, isn't it?<!--pagination-->
And I tried to use different method to page it.
Can you see it?
DATA;
//当前文章页
$page = 0;
//初始文章长度
$length = 0;
//分页长度
$perpage = 160;
//显示在页面的代码
$link = '';
//分割后的数组
$strArr = array();
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$length = strlen($data);
//按字数分割
// $str = str_split($data, $perpage);
//按字符分割
$delimiter = "\n";
// $delimiter = '<--pagination-->';
$strArr = explode($delimiter, $data);
$strNum = count($strArr);
$content = $strArr[$page];
if ($strNum > 1) {
if ($page != 0) {
$link .= '<a href="?page=0">首页</a>';
} else {
$link .= '<span>首页</span>';
}
for ($n = 0; $n < $strNum; $n++) {
if ($n == $page) {
$link .= '<span>' . ($n + 1) . '</span>';
} else {
$link .= "<a href='?page={$n}'>" . ($n + 1) . "</a>";
}
}
$link .= '';
if ($page != ($strNum - 1)) {
$link .= "<a href='?page=" . ($strNum - 1) . "'>尾页</a>";
} else {
$link .= '<span>尾页</span>';
}
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
font-family: '微软雅黑';
}
.link a, span {
margin: 1px;
padding: 1px;
}
.link span {
color: #777;
}
.link a {
color: #26A2DA;
text-decoration: none;
}
</style>
<title>测试文章分页</title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>
<hr />
<p class="link"><?php echo $link; ?></p>
</body>
</html>
下一篇:PHP 查找字符串常用函数介绍