Long article automatic/manual page-type

Source: Internet
Author: User
Tags foreach chr manual strlen

<?php
/*
* Long article Pagination class
* @package Cutpage
* @author yytcpt (no shadow)
* @version 2008-03-27
* @copyrigth http://www.d5s.cn/
*/
Class cutpage{
var $pagestr; The content of the Shard
var $pagearr; The array format of the text being slit
var $sum _word; The total number of words (Chinese characters in the UTF-8 format are also included)
var $sum _page; Total pages
var $page _word; How many words a page
var $cut _tag; Automatic page Breaks
var $cut _custom; Manual page Breaks
var $ipage; The number of pages in the current Shard, page
var $url;
function __construct () {
$this->page_word = 1000;
$this->cut_tag = Array ("</table>", "</div>", "</p>", "<br/>", "".) ", "。 ", ".", "! ", "......", "? ", ",");
$this->cut_custom = "{nextpage}";
$tmp _page = intval (Trim ($_get["ipage"]);
$this->ipage = $tmp _page>1 $tmp _page:1;
}
Total number of words in statistics
function Get_page_word () {
$this->sum_word = $this->strlen_utf8 ($this->pagestr);
return $this->sum_word;
}
/* Statistics UTF-8 encoded character length
* One Chinese, one English all for one word
*/
function Strlen_utf8 ($STR) {
$i = 0;
$count = 0;
$len = strlen ($STR);
while ($i < $len) {
$CHR = Ord ($str [$i]);
$count + +;
$i + +;
if ($i >= $len)
Break
if ($CHR & 0x80) {
$CHR <<= 1;
while ($CHR & 0x80) {
$i + +;
$CHR <<= 1;
}
}
}
return $count;
}
Set up automatic page-splitting symbols
function Set_cut_tag ($tag _arr=array ()) {
$this->cut_tag = $tag _arr;
}
Set manual page Breaks
function Set_cut_custom ($cut _str) {
$this->cut_custom = $cut _str;
}
function Show_cpage ($ipage =0) {
$this->cut_str ();
$ipage = $ipage? $ipage: $this->ipage;
return $this->pagearr[$ipage];
}
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 word count 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); Find the location of the first page break in reverse
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 more than the total number of words
$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); Page_word characters after the word Cut_start
$cut _tmp = Strrpos ($str _tmp, $v); Find out the position of the first page break from the Cut_start word, page_word, and backwards.
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;
}
Displays the previous one, the next one
function Show_prv_next () {
$this->set_url ();
if ($this->sum_page>1 and $this->ipage< $this->sum_page) {
$str = "<a href= '". $this->url. ($this->ipage+1). "' > next page </a> ";
}
if ($this->sum_page>1 and $this->ipage>1) {
$str. = "<a href= '". $this->url. ($this->ipage-1). "' > Prev </a> ";
}
return $str;
}
function Show_page_select () {
if ($this->sum_page>1) {
$str = "&nbsp; <select onchange=\ "location.href=this.options[this.selectedindex].value\" >;
For ($i =1 $i <= $this->sum_page; $i + +) {
$str. = "<option value= '". $this->url. $i. "' ". ($this->ipage) = = $i? "Selected= ' selected '": "" ". > ". $i." Page </option> ";
}
$str. = "</select>";
}
return $str;
}
function Show_page_select_wap () {
if ($this->sum_page>1) {
$str = "<select ivalue= '". ($this->ipage-1). "' > ";
For ($i =1 $i <= $this->sum_page; $i + +) {
$str. = "<option onpick= '". $this->url. $i. "' > ". $i." Section </option> ";
}
$str. = "</select>";
}
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;
}
}
?>

If you have added new features, or have improved, please share with us.

Long article pagination class, you can specify a page break manually, or you can have the program automatically pagination.

Instance code: Please test with UTF-8 file encoding.

code: <?php
     Include (' cutpage.php ');
    header ("Content-type:text/html;charset=utf-8");//Set page encoding
     //custom Long article string, which can contain HTML code, if there is a manual page break {nextpage} in the string, prioritize page breaks by manual page break
     $content = file_get_ Contents (' Text.txt ');
     $ipage = $_get["IPage"]? Intval ($_get["IPage"]): 1;
     $CP = new Cutpage ();
     $CP->pagestr = $content;
     $CP->cut_str ();
    echo $CP->pagearr[$ipage-1]. "     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.