When the content of the article, in order to facilitate reading and page display we generally display the content page. In general, paging is a static file that generates multiple pages of content when an article is published in the background. In this paper, we combine examples to explain the use of PHP dynamic to the long article content paging processing.
View Demo Download source code
How to split pages
Manual paging: When you edit content, you typically add special paging tags, such as {pages}, and the PHP program processes pagination based on page breaks to generate different static pages. This paging method is accurate, but requires manual page breaks to manually add a lot of work.
Automatic paging: The PHP program pages the content according to a set page break, and then generates a different static page. This method is highly efficient and requires a high level of processing of different HTML code tags.
Front-end JS paging: Use JavaScript to intercept the long article content, according to the request to show different segmented content, to achieve the paging effect. This method will read the content at once, by the front-end JS processing paging, experience good.
The example code in this article explains the use of PHP to page long article content, automatic and manual paging. As for generating static HTML pages is not covered in the scope of this article, we will specifically explain the generation of static aspects of the article introduction.
Pagination class
Page_word = $page _word; $this->cut_tag = Array ("", "", "", "
", "”。 ", "。 ", ".", "! ", "......", "? ", ","); $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 number of words 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); Reverse Lookup FirstThe location of the page break if ($cut _start) {$page _arr[$i + +] = substr ($this->page STR, 0, $cut _start). $v; $cut _start = $cut _start + strlen ($v); Break }} if (($cut _start+ $this->page_word) >= $str _len_word) {//If the total number of words is exceeded $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); Take the Page_word characters after the first cut_start character $cut _tmp = Strrpos ($str _tmp, $v); Find the position of the first page break from the Page_word word after Cut_start, 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->pag E_word) > $str _len_word) {$page _arr[$i + +] = substr ($this->pagestr, $cut _start, $this-&G T;page_word); }}}} $this->sum_page = count ($page _arr); Total pages $this->pagearr = $page _arr; return $page _arr; }//Displays the previous bar, 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. = "". $i. " ";} Else{$str. = "URL. $i." > ". $i." ";}} 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 trouble of different HTML tags for paging. If the content is set to page break {nextpage}, the content is automatically paginated by page breaks.
Calling a paging class
We assume that the content of the file Text.txt is read, and the actual project should be the form submitting long content or reading the contents of the database related table. It then instantiates the paging class and then invokes the corresponding paging content and output, along with the output paging bar, based on the current page.
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.