When the content of the article, in order to facilitate reading and page display we generally will content pagination to display. Generally, paging is a static file that generates multiple pagination after a post is published in the background. In this paper, we combine examples to explain the use of PHP dynamic content of the long article paging processing.
Manual paging: Generally when editing content to add special paging tags, such as {pages}, after submission, PHP program will be based on page break processing pagination, generate a different static page. This paging method is accurate, but requires manual page breaks to be manually added, with a lot of work.
Automatic paging: The PHP program pages the content according to the set page break, and then generates a different static page. This method is highly efficient and requires high requirements for handling different HTML code tags.
Front-end JS pagination: Use JavaScript will long article content interception section, according to the request to display different sections of content, to achieve the paging effect. This method once content read, from the front-end JS processing paging, experience good.
This example code illustrates the use of PHP will be a long article content paging, you can automatically and manually paging. As for the generation of static HTML page is not covered in this article, we will specifically explain the generation of static aspects of the article introduction.
Paging class
<?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 = intval (Trim ($_get["ipage"]);
$this->ipage = $tmp _page>1 $tmp _page:1;
$this->pagestr = $pagestr;
}
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 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;
}
}
?>
The above Cutpage class can handle content paging very well, and can handle the hassle of paging with different HTML tags. If the content is set to page break {nextpage}, the content is automatically paginated by page breaks.
Calling a paging class
We assume that we read the article content in the file Text.txt, the actual project should be the form submit long content or read the contents of the database related tables. The paging class is then instantiated, and then the content of the page is called according to the current page and output, along with the paging bar.
<?php
$content = file_get_contents (' text.txt ');
$ipage = $_get["IPage"]? Intval ($_get["IPage"]): 1;
$CP = new Cutpage ($content);
$page = $CP->cut_str ();
echo $page [$ipage-1];
echo $CP->pagenav ();
?>
It's worth noting that using a unified UTF-8 file encoding will make your coding work smoother.