PHP implements long-article paging instance code (with source code), _ PHP Tutorial

Source: Internet
Author: User
PHP implements long-article paging instance code (with source code ),. PHP implements long-article paging instance code (with source code). when the content of the article is relatively long, in order to better meet the user experience, we will display the article content by page, in general, pagination is PHP's code for implementing long-article paging instances (with source code ),

When the content of the article is relatively long, in order to better meet the user experience, we will display the content of the article by page, generally, pagination generates static files after multiple pagination when publishing an article in the background. This article uses php in combination with examples to dynamically process long articles by page.

View demo Source Code Download

How to paging

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

<? Php/** long article paging class */class cutpage {private $ pagestr; // The split content is private $ pagearr; // The array format of the split text is private $ sum_word; // total words (Chinese characters in UTF-8 format also include) private $ sum_page; // total number of pages private $ page_word; // The number of words on a page private $ cut_tag; // automatic pagination operator private $ cut_custom; // manual pagination operator private $ ipage; // The number of pages to be split, page number 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 ); // obtain 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 the number of 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 _ The word cutStr is func. foreach ($ this-> cut_tag as $ v) {$ cut_start = strrpos ($ str_first, $ v ); // reverse query the position 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_star T + $ this-> page_word) <$ str_len_word) {foreach ($ this-> cut_tag as $ v) {$ str_tmp = substr ($ this-> pagestr, $ cut_start, $ this-> page_word); // The page_word character after the cut_start character $ cut_tmp = strrpos ($ str_tmp, $ v ); // find the position of the first paging character if ($ cut_tmp) between the page_word characters after the cut_start character) {$ 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-> page_word );}}}} $ this-> sum_page = count ($ page_arr); // The total number of pages $ this-> pagearr = $ page_arr; return $ page_arr;} // display the previous 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.

<?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 use method, does not split sections, pages at the bottom
  • Pagination of php article content and generate corresponding htm static page code
  • PHP text article paging code by tag or length (no database involved)
  • Code for pagination of long articles in php

When the content of the article is relatively long, in order to better meet the user experience, we will display the content of the article by page, while the general paging 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.