PHP implementation of long article page instance code (with source), _php tutorial

Source: Internet
Author: User

PHP implementation of long article page instance code (with source),


When the content of the article is longer, in order to better meet the user experience, we will display the content of the article page processing, and the general paging process is published in the background when the article will be submitted to generate a number of pages after the static file. By using this article, we use PHP to page up the content of long articles dynamically.

View Results Demo Source download

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

<?php */* Long article Page class */class cutpage{private $pagestr;//The segmented content private $pagearr;//The array format of the sliced text private $sum _word;//Total number of words ( The Chinese characters in the UTF-8 format are also included) private $sum _page; Total pages Private $page _word; A page How many words private $cut _tag; Automatic page break private $cut _custom; Manual page Break private $ipage; The number of pages currently sliced, the first few pages private $url; function __construct ($pagestr, $page _word=1000) {$this->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 that are 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 function foreach in Func.global ($this->cut_tag as $v) {$cut _start = Strrpos ($str _first, $v);//Reverse Find the location of the first page break if ($cut _start) {$page _arr[$i + +] = substr ($this->pagestr, 0, $c Ut_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->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 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.

<?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 ();?>

Articles you may be interested in:

    • PHP long article paging function with the use of the method, will not split the paragraph, the page in the bottom
    • PHP article content paging and generating corresponding HTM static page code
    • PHP text article pagination code by tag or length (no database involved)
    • PHP Medium-Length article Pagination display implementation code

http://www.bkjia.com/PHPjc/1098693.html www.bkjia.com true http://www.bkjia.com/PHPjc/1098693.html techarticle PHP Implementation of long article page instance code (with source), when the article content is longer, in order to better meet the user experience, we will be the article content pagination display processing, and the General page processing is ...

  • 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.