PHP implementation of long article pagination instance code (with source) _php instance

Source: Internet
Author: User
Tags html tags

When the content of the article is relatively long, in order to better meet the user experience, we will page the content of the display processing, and general paging is the post in the background when the submission of the content generated more than one page after the static file. In this paper, we use the example of PHP dynamically to page the content of long articles.

View effect Demo Source download

How to page pagination

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 split content is private $pagearr;//The array format of the Shard text is private $sum _word ; The total number of words (UTF-8 format of the Chinese characters are also included) private $sum _page; Total pages Private $page _word; A page How many words private $cut _tag; Automatic page breaks private $cut _custom; Manual page Break private $ipage; 
The number of pages in the current Shard, the first few pages 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; 
The function cut_str () {$str _len_word = strlen ($this->pagestr);//Get the total number of characters used strlen $i = 0; 
if ($str _len_word<= $this->page_word) {//If the total word count is less than one page display word $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 foreach in Func.global ($t His->cut_tag as $v) {$cut _start = Strrpos ($str _first, $v);//Reverse Find position of first page break 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 word $page _arr[$i + +] = substr ($this->pagestr, $CU 
T_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); After the word cut_start page_word characters $cut _tmp = Strrpos ($str _tmp, $v); 
Find out the position of the first page break from the Cut_start word page_word, 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 number of pages $this->pagearr = $page _arr; 
return $page _arr; 
//Display the previous one, the next 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 (); 
? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.