PHP long article paging example-PHP source code

Source: Internet
Author: User
The article page is usually a small process of article content too long to facilitate reading. Let's take a look at an example of using PHP to pagination long articles. The details are as follows. The article page is usually a small process of article content too long to facilitate reading. Let's take a look at an example of using PHP to pagination long articles. The details are as follows.

Script ec (2); script

For the convenience of reading and page display, we usually display the content on pages. Generally, pagination generates static files after multiple pagination when publishing an article in the background. This article uses examples to explain how to use PHP to dynamically process long articles by page.

Manual pagination: Special pagination tags are usually added during content editing, such as {pages}. After submission, the PHP program processes pagination Based on the pagination character to generate different static pages. This paging method provides accurate paging, but requires manual addition of paging characters, resulting in heavy workload.
Automatic paging: the PHP program will paginate the content based on the configured pagination character and then generate different static pages. This method is highly efficient and requires high requirements for processing tags of different html codes.
Front-end JS paging: uses Javascript to intercept long articles and display different sections based on requests to achieve paging effect. This method reads the content at a time, and the front-end js processes the page.
The example code in this article explains how to use PHP to pagination long article content, which can be automatically and manually paged. Generating static html pages is not covered in this article. We will introduce the generation of static pages later.
Paging type

/*
* Long article Paging
*/
Class cutpage {
Private $ pagestr; // split content
Private $ pagearr; // array format of the split text
Private $ sum_word; // total words (Chinese Characters in UTF-8 format are also included)
Private $ sum_page; // the total number of pages.
Private $ page_word; // Number of words on a page
Private $ cut_tag; // automatic paging character
Private $ cut_custom; // Manual paging character
Private $ ipage; // The number of pages to be split.
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 obtained using strlen.
$ I = 0;
If ($ str_len_word <= $ this-> page_word) {// if the total number of words is less than one page, the number of words is displayed.
$ 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 cutStr is a function in func. global.
Foreach ($ this-> cut_tag as $ v ){
$ Cut_start = strrpos ($ str_first, $ v); // reverse query the location of the first paging character
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 the total number of words exceeds
$ 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 character after the cut_start character
$ Cut_tmp = strrpos ($ str_tmp, $ v); // locate the position of the first page_word after the word cut_start is obtained.
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); // the total number of pages.
$ This-> pagearr = $ page_arr;
Return $ page_arr;
}
// Display the previous and 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 handle different html tags, causing paging troubles. If the content has a pagination character {nextpage}, the content is automatically paged by pagination character first.
Call paging class
We have read the text in text.txt. In actual projects, it should be the form that submits long content or reads the content of the database-related tables. The pagination class is instantiated, and the corresponding pagination content is called based on the current page, and the pagination entries are output.
$ 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 is worth noting that the use of a unified UTF-8 file encoding will make your coding work more smoothly.

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.