thinkphp Long article pagination
Require "page.class.php";
$neirong = content Content content contents content content content content content content content;
$content = file_get_contents ($neirong);
$ipage = Isset ($_get["IPage"])? Intval ($_get["IPage"]): 1;
$CP = new Cutpage ($content);
$page = $CP->cut_str ();
echo $page [$ipage-1];
Echo '. $CP->pagenav (). '';
Class Classes
page.class.php
<?php
/*
* Long article Pagination class
*/
Class cutpage{
Private $pagestr; The content of the Shard
Private $pagearr; The array format of the text being slit
Private $sum _word; The total number of words (Chinese characters in the UTF-8 format are also included)
Private $sum _page; Total pages
Private $page _word; How many words a page
Private $cut _tag; Automatic page Breaks
Private $cut _custom; Manual page Breaks
Private $ipage; The number of pages in the current Shard, page
Private $url;
function __construct ($pagestr, $page _word=1000) {
$this->page_word = $page _word;
$this->cut_tag = Array ("</table>", "</div>", "</p>", "<br/>", "".) ", "。 ", ".", "! ", "......", "? ", ",");
$this->cut_custom = "{nextpage}";
$tmp _page = isset ($_get["IPage"])? Intval ($_get["IPage"]): 1;
$this->ipage = $tmp _page>1 $tmp _page:1;
$this->pagestr = $pagestr;
}
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;
return $page _arr;
}
Displays the previous one, the next one
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. = "<a href= ' class= ' cur ' >". $i. " </a> ";
}else{
$str. = "<a href= '". $this->url. $i. "' > ". $i." </a> ";
}
}
return $str;
}
function Show_prv_next2 () {
$this->set_url ();
$str = ';
if ($this->sum_page>1 and $this->ipage>1) {
$str. = "<a href= '". $this->url. ($this->ipage-1). "' > Prev </a> ";
}
if ($this->sum_page>1 and $this->ipage< $this->sum_page) {
$str. = "<a href= '". $this->url. ($this->ipage+1). "' > next page </a> ";
}
return $str;
}
function Show_page_select () {
if ($this->sum_page>1) {
$str = "<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;
}
}
?>
PHP Long article pagination display
This example code illustrates the use of PHP will be a long article content paging, you can automatically and manually paging. As for the generation of static HTML page is not covered in this article, we will specifically explain the generation of static aspects of the article introduction.
Paging class
<?php
/*
* Long article Pagination class
*/
Class cutpage{
Private $pagestr; The content of the Shard
Private $pagearr; The array format of the text being slit
Private $sum _word; The total number of words (Chinese characters in the UTF-8 format are also included)
Private $sum _page; Total pages
Private $page _word; How many words a page
Private $cut _tag; Automatic page Breaks
Private $cut _custom; Manual page Breaks
Private $ipage; The number of pages in the current Shard, page
Private $url;
function __construct ($pagestr, $page _word=1000) {
$this->page_word = $page _word;
$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;
$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 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;
return $page _arr;
}
Displays the previous one, the next one
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. = "<a href= ' class= ' cur ' >". $i. " </a> ";
}else{
$str. = "<a href= '". $this->url. $i. "' > ". $i." </a> ";
}
}
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 hassle of paging with different HTML tags. If the content is set to page break {nextpage}, the content is automatically paginated by page breaks.
Calling a paging class
We assume that we read the article content in the file Text.txt, the actual project should be the form submit long content or read the contents of the database related tables. The paging class is then instantiated, and then the content of the page is called according to the current page and output, along with the paging bar.
<?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 ();
?>
It's worth noting that using a unified UTF-8 file encoding will make your coding work smoother.