PHP 文本文章分页代码 按标记或长度(不涉及数据库)
程序员文章站
2022-09-10 14:49:21
实例代码: 复制代码 代码如下:
实例代码:
<?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>
下一篇: 闲鱼店群秘籍,让你从小白到大咖的方法