PHP long article automatic/manual paging class

Source: Internet
Author: User

<? Php <br/>/* <br/> * long article paging class <br/> * @ package cutpage <br/> * @ author yytcpt (Shadow) <br/> * @ version 2008-03-27 <br/> * @ copyri.pdf http://www.d5s.cn/<br/> */<br/> class cutpage {<br/> var $ pagestr; // split content <br/> var $ pagearr; // array format of the split text <br/> var $ sum_word; // total words (Chinese Characters in UTF-8 format are also included) <br/> var $ sum_page; // total number of pages <br/> var $ page_word; // Number of words on a page <br/> var $ cut_tag; // automatic paging character <br/> var $ cut_custom; // Manual paging character <br/> VaR $ Ipage; // The number of pages currently split. Page number <br/> var $ URL; <br/> function _ construct () {<br/> $ this-> page_word = 1000; <br/> $ this-> cut_tag = array ("</table>", "</div> ", "</P>", "<br/> ","". ",". ",".","! ","...... ","? ",", "); <Br/> $ this-> cut_custom =" {nextpage }"; <br/> $ tmp_page = intval (TRIM ($ _ Get ["ipage"]); <br/> $ this-> ipage = $ tmp_page> 1? $ Tmp_page: 1; <br/>}< br/> // count the total number of words <br/> function get_page_word () {<br/> $ this-> sum_word = $ this-> strlen_utf8 ($ this-> pagestr); <br/> return $ this-> sum_word; <br/>}< br/>/* count the length of characters in UTF-8 encoding <br/> * a Chinese character, each English word <br/> */<br/> function strlen_utf8 ($ Str) {<br/> $ I = 0; <br/> $ COUNT = 0; <br/> $ Len = strlen ($ Str); <br/> while ($ I <$ Len) {<br/> $ CHR = ord ($ STR [$ I]); <br/> $ count ++; <br/> $ I ++; <br/> If ($ I >=$ Len) <br/> break; <br/> if ($ CHR & 0x80) {<br/> $ CHR <= 1; <br/> while ($ CHR & 0x80) {<br/> $ I ++; <br/> $ CHR <= 1; <br/>}< br/> return $ count; <br/>}< br/> // set the automatic paging symbol <br/> function set_cut_tag ($ tag_arr = array ()) {<br/> $ this-> cut_tag = $ tag_arr; <br/>}< br/> // sets the manual paging character <br/> function set_cut_custom ($ cut_str) {<br/> $ this-> cut_custom = $ cut_str; <br/>}< br/> funct Ion show_cpage ($ ipage = 0) {<br/> $ this-> cut_str (); <br/> $ ipage = $ ipage? $ Ipage: $ this-> ipage; <br/> return $ this-> pagearr [$ ipage]; <br/>}< br/> function cut_str () {<br/> $ str_len_word = strlen ($ this-> pagestr); // obtain the total number of characters obtained using strlen <br/> $ I = 0; <br/> if ($ str_len_word <= $ this-> page_word) {// if the total number of words is less than one page, the number of words displayed <br/> $ page_arr [$ I] = $ this-> pagestr; <br/>} else {<br/> If (strpos ($ this-> pagestr, $ this-> cut_custom )) {<br/> $ page_arr = explode ($ this-> cut_custom, $ this-> pagestr); <B R/>}else {<br/> $ str_first = substr ($ this-> pagestr, 0, $ this-> page_word); // The cutstr text in 0-page_word is func. functions in global <br/> foreach ($ this-> cut_tag as $ v) {<br/> $ cut_start = strrpos ($ str_first, $ V ); // reverse query the location of the first paging character <br/> if ($ cut_start) {<br/> $ page_arr [$ I ++] = substr ($ this-> pagestr, 0, $ cut_start ). $ V; <br/> $ cut_start = $ cut_start + strlen ($ V); <br/> break; <br/>}< br/> If ($ cut_start + $ t His-> page_word) >=$ str_len_word) {// if the total number of words exceeds <br/> $ page_arr [$ I ++] = substr ($ this-> pagestr, $ cut_start, $ this-> page_word); <br/>}else {<br/> while ($ cut_start + $ this-> page_word) <$ str_len_word) {<br/> foreach ($ this-> cut_tag as $ v) {<br/> $ str_tmp = substr ($ this-> pagestr, $ cut_start, $ this-> page_word); // The page_word character after the cut_start character <br/> $ cut_tmp = strrpos ($ str_tmp, $ V ); // after finding the word cut_start, page_wor Find the location of the first paging character in reverse direction between D characters <br/> if ($ cut_tmp) {<br/> $ page_arr [$ I ++] = substr ($ str_tmp, 0, $ cut_tmp ). $ V; <br/> $ cut_start = $ cut_start + $ cut_tmp + strlen ($ V); <br/> break; <br/>}< br/> If ($ cut_start + $ this-> page_word)> $ str_len_word) {<br/> $ page_arr [$ I ++] = substr ($ this-> pagestr, $ cut_start, $ this-> page_word ); <br/>}< br/> $ this-> sum_page = count ($ page_arr );/ /Total number of pages <br/> $ this-> pagearr = $ page_arr; <br/>}< br/> // display the previous and next pages <br/> function show_prv_next () {<br/> $ this-> set_url (); <br/> if ($ this-> sum_page> 1 and $ this-> ipage <$ this-> sum_page) {<br/> $ STR = "<a href = '". $ this-> URL. ($ this-> ipage + 1 ). "'> next page </a>"; <br/>}< br/> if ($ this-> sum_page> 1 and $ this-> ipage> 1) {<br/> $ Str. = "<a href = '". $ this-> URL. ($ this-> ipage-1 ). "'> previous page </a>"; <br/>}< br/> return $ STR; <Br/>}< br/> function show_page_select () {<br/> if ($ this-> sum_page> 1) {<br/> $ STR = "<select onchange =/" location. href = This. options [this. selectedindex]. value/">"; <br/> for ($ I = 1; $ I <= $ this-> sum_page; $ I ++) {<br/> $ Str. = "<option value = '". $ this-> URL. $ I. "'". ($ this-> ipage) = $ I? "Selected = 'selected '":""). "> Number ". $ I. "page </option>"; <br/>}< br/> $ Str. = "</SELECT>"; <br/>}< br/> return $ STR; <br/>}< br/> function show_page_select_wap () {<br/> if ($ this-> sum_page> 1) {<br/> $ STR = "<select ivalue = '". ($ this-> ipage-1 ). "'>"; <br/> for ($ I = 1; $ I <= $ this-> sum_page; $ I ++) {<br/> $ Str. = "<option onpick = '". $ this-> URL. $ I. "'> Number ". $ I. "Section </option>"; <br/>}< br/> $ Str. = "</SELECT>"; <br/>}< br /> Return $ STR; <br/>}< br/> function set_url () {<br/> parse_str ($ _ server ["QUERY_STRING"], $ arr_url ); <br/> unset ($ arr_url ["ipage"]); <br/> If (emptyempty ($ arr_url) {<br/> $ STR = "ipage = "; <br/>} else {<br/> $ STR = http_build_query ($ arr_url ). "& ipage ="; <br/>}< br/> $ this-> url = "http ://". $ _ server ["http_host"]. $ _ server ["php_self"]. "? ". $ STR; <br/>}< br/>?>

Instance code: perform a test with the UTF-8's file code.

Include ('cutpage. PHP '); <br/> header ("Content-Type: text/html; charset = UTF-8 "); // set the page encoding <br/> // custom long article string, which can contain HTML code, if the string contains the manual paging character {nextpage}, the manual paging is prioritized <br/> $ content = file_get_contents('text.txt '); <br/> $ ipage = $ _ Get ["ipage"]? Intval ($ _ Get ["ipage"]): 1; <br/> $ CP = new cutpage (); <br/> $ CP-> pagestr = $ content; <br/> $ CP-> cut_str (); <br/> echo $ CP-> pagearr [$ ipage-1]. "<HR/>"; <br/> echo $ CP-> show_prv_next ();

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.