Content.txt
This file contains content, if you are combined with the MySQL database, as long as you replace this sentence with the content of your database.
index.php file
<link rel= "stylesheet" type= "Text/css" href= "Http:///jquery/css/common.css"/>
<style type= "Text/css" >
. demo{width:80%;margin:50px auto 10px auto;padding:10px;}
. Demo P{line-height:30px;text-indent:2em}
. Demo h3{font-size:24px;text-align:center;padding:10px}
@media (max-width:480px) {
. demo{width:360px;margin:50px auto 10px auto;padding:10px;}
. Demo img{width:90%}
. Demo H3{font-size:1.5em;line-height:1.9em}
}
. pages{text-align:center;border-top:1px solid #d3d3d3; margin-top:10px padding-top:10px}
. pages a{display:inline-block;margin:4px;padding:4px 8px;background: #f9f9f9; border:1px solid #d9d9d9}
. pages A.cur{cursor:default;background: #d3d3d3; color: #434343}
. Pages A.cur:hover{text-decoration:none}
</style>
<div class= "Head" >
<div class= "Head_inner clearfix" >
<ul id= "NAV" >
<li><a href= "http://" > Home </a></li>
<li><a href= "http:///templates" > site templates </a></li>
<li><a href= "Http:///js" > Web effects </a></li>
<li><a href= "http:///php" >PHP</a></li>
<li><a href= "Http:///site" > Featured URL </a></li>
</ul>
<a class= "logo" href= "http://" ><img src= "http:///Public/images/logo.jpg" alt= "material Fire logo"/></a>
</div>
</div>
<div class= "Container" >
<div class= "Demo" >
<H2 class= "title" ><a href= "http:///js/639.html" > Tutorial: Php long article pagination tutorial and demo </a></h2>
<h3> Tens of millions of people will not work in the next 10 years? </h3>
<?php
Include (' page.class.php ');
A custom long article string that can contain HTML code, and if a manual page break {nextpage} is in the string, the page breaks are prioritized by a manual page break
$content = file_get_contents (' Content.txt ');
$ipage = Isset ($_get["IPage"])? Intval ($_get["IPage"]): 1;
$CP = new Cutpage ($content);
$page = $CP->cut_str ();
echo $page [$ipage-1];
Echo ' <div class= ' pages > '. $CP->pagenav (). ' </div> ';
?>
</div>
</div>
Long article pagination class page.class.php
<?php
/*
* Long article Pagination class
*/
Class cutpage{
Private $pagestr; The content of the Shard
Private $pagearr; The array format of the text being slit
Private $sum _word; The total number of words (Chinese characters in the UTF-8 format are also included)
Private $sum _page; Total pages
Private $page _word; How many words a page
Private $cut _tag; Automatic page Breaks
Private $cut _custom; Manual page Breaks
Private $ipage; The number of pages in the current Shard, page
Private $url;
function __construct ($pagestr, $page _word=1000) {
$this->page_word = $page _word;
$this->cut_tag = Array ("</table>", "</div>", "</p>", "<br/>", "".) ", "。 ", ".", "! ", "......", "? ", ",");
$this->cut_custom = "{nextpage}";
$tmp _page = isset ($_get["IPage"])? Intval ($_get["IPage"]): 1;
$this->ipage = $tmp _page>1 $tmp _page:1;
$this->pagestr = $pagestr;
}
Total number of words in statistics
function Get_page_word () {
$this->sum_word = $this->strlen_utf8 ($this->pagestr);
return $this->sum_word;
}
/* Statistics UTF-8 encoded character length
* One Chinese, one English all for one word
*/
function Strlen_utf8 ($STR) {
$i = 0;
$count = 0;
$len = strlen ($STR);
while ($i < $len) {
$CHR = Ord ($str [$i]);
$count + +;
$i + +;
if ($i >= $len)
Break
if ($CHR & 0x80) {
$CHR <<= 1;
while ($CHR & 0x80) {
$i + +;
$CHR <<= 1;
}
}
}
return $count;
}
Set up automatic page-splitting symbols
function Set_cut_tag ($tag _arr=array ()) {
$this->cut_tag = $tag _arr;
}
Set manual page Breaks
function Set_cut_custom ($cut _str) {
$this->cut_custom = $cut _str;
}
function Show_cpage ($ipage =0) {
$this->cut_str ();
$ipage = $ipage? $ipage: $this->ipage;
return $this->pagearr[$ipage];
}
function Cut_str () {
$str _len_word = strlen ($this->pagestr); Gets the total number of characters obtained using strlen
$i = 0;
if ($str _len_word<= $this->page_word) {//If the total word count is less than one page display words
$page _arr[$i] = $this->pagestr;
}else{
if (Strpos ($this->pagestr, $this->cut_custom)) {
$page _arr = explode ($this->cut_custom, $this->pagestr);
}else{
$str _first = substr ($this->pagestr, 0, $this->page_word); 0-page_word text Cutstr as a function in Func.global
foreach ($this->cut_tag as $v) {
$cut _start = Strrpos ($str _first, $v); Find the location of the first page break in reverse
if ($cut _start) {
$page _arr[$i + +] = substr ($this->pagestr, 0, $cut _start). $v;
$cut _start = $cut _start + strlen ($v);
Break
}
}
if ($cut _start+ $this->page_word) >= $str _len_word) {//If more than the total number of words
$page _arr[$i + +] = substr ($this->pagestr, $cut _start, $this->page_word);
}else{
while (($cut _start+ $this->page_word) < $str _len_word) {
foreach ($this->cut_tag as $v) {
$str _tmp = substr ($this->pagestr, $cut _start, $this->page_word); Page_word characters after the word Cut_start
$cut _tmp = Strrpos ($str _tmp, $v); Find out the position of the first page break from the Cut_start word, page_word, and backwards.
if ($cut _tmp) {
$page _arr[$i + +] = substr ($str _tmp, 0, $cut _tmp). $v;
$cut _start = $cut _start + $cut _tmp + strlen ($v);
Break
}
}
}
if ($cut _start+ $this->page_word) > $str _len_word) {
$page _arr[$i + +] = substr ($this->pagestr, $cut _start, $this->page_word);
}
}
}
}
$this->sum_page = count ($page _arr); Total pages
$this->pagearr = $page _arr;
return $page _arr;
}
Displays the previous one, the next one
function Pagenav () {
$this->set_url ();
$str = ';
$str. = $this->ipage. /'. $this->sum_page;
For ($i =1 $i <= $this->sum_page; $i + +) {
if ($i = = $this->ipage) {
$str. = "<a href= ' class= ' cur ' >". $i. " </a> ";
}else{
$str. = "<a href= '". $this->url. $i. "' > ". $i." </a> ";
}
}
return $str;
}
function Show_prv_next2 () {
$this->set_url ();
$str = ';
if ($this->sum_page>1 and $this->ipage>1) {
$str. = "<a href= '". $this->url. ($this->ipage-1). "' > Prev </a> ";
}
if ($this->sum_page>1 and $this->ipage< $this->sum_page) {
$str. = "<a href= '". $this->url. ($this->ipage+1). "' > next page </a> ";
}
return $str;
}
function Show_page_select () {
if ($this->sum_page>1) {
$str = "<select onchange= ' Location.href=this.options[this.selectedindex].value ' >";
For ($i =1 $i <= $this->sum_page; $i + +) {
$str. = "<option value= '". $this->url. $i. "' ". ($this->ipage) = = $i? "Selected= ' selected '": "" ". > ". $i." Page </option> ";
}
$str. = "</select>";
}
return $str;
}
function Show_page_select_wap () {
if ($this->sum_page>1) {
$str = "<select ivalue= '". ($this->ipage-1). "' > ";
For ($i =1 $i <= $this->sum_page; $i + +) {
$str. = "<option onpick= '". $this->url. $i. "' > ". $i." Section </option> ";
}
$str. = "</select>";
}
return $str;
}
function Set_url () {
Parse_str ($_server["query_string"), $arr _url);
unset ($arr _url["IPage"]);
if (Empty ($arr _url)) {
$str = "ipage=";
}else{
$str = Http_build_query ($arr _url). " &ipage= ";
}
$this->url = "http://". $_server["Http_host"].$_server["Php_self"]. $STR;
}
}
?>