PHP text article paging code by mark or length (no database involved)
- 2020-05-17 04:59:07
- OfStack
Example code:
<?php
/**
* **********************************************************
* Read Me
* The article page
*
* Paging mode, you can page by word count, page by line feed, page by special marks, etc
* Actually, the idea is 1 Like, just press it 1 Fixed law put in 1 An array
* Then according to the url Pass in the parameter to get a fragment
* You could write it 1 Three powerful functions are saved for a rainy day
*
* As an aside: many editors have insert page buttons that display pages with inserted code
*
* 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 that needs to be paged
$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;
// Current article page
$page = 0;
// Initial article length
$length = 0;
// The length of the page
$perpage = 160;
// Displays the code on the page
$link = '';
// The split array
$strArr = array();
$page = isset($_GET['page']) ? intval($_GET['page']) : 0;
$length = strlen($data);
// Alphanumeric segmentation
// $str = str_split($data, $perpage);
// Character segmentation
$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"> Home page </a>';
} else {
$link .= '<span> Home page </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) . "'> back </a>";
} else {
$link .= '<span> back </span>';
}
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<style type="text/css">
body {
font-family: ' Microsoft jas black ';
}
.link a, span {
margin: 1px;
padding: 1px;
}
.link span {
color: #777;
}
.link a {
color: #26A2DA;
text-decoration: none;
}
</style>
<title> Test article pagination </title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<p><?php echo $content; ?></p>
<hr />
<p class="link"><?php echo $link; ?></p>
</body>
</html>