方便快捷的PHP長(zhǎng)文章分頁(yè)函數(shù)
作者:佚名 
  我們?cè)谶@篇文章中為大家展現(xiàn)了PHP長(zhǎng)文章分頁(yè)函數(shù)的代碼示例,希望能夠通過本文掌握分頁(yè)代碼的運(yùn)用,靈活運(yùn)用,方便編程。
 PHP程序員們?cè)诰帉懗绦虼a的時(shí)候,通常都會(huì)用到文章的分頁(yè)等功能的實(shí)現(xiàn)。每次編寫不同的程序都需要編寫一遍文章分頁(yè)的代碼,比較瑪法。#t#
今天我們就為大家寫了一段PHP長(zhǎng)文章分頁(yè)函數(shù),方便大家的代碼編寫:
- < ?php
 - function ff_page($content,$page)
 - {
 - global $expert_id;
 - $PageLength = 2000; //每頁(yè)字?jǐn)?shù)
 - $CLength = strlen($content);
 - $PageCount = floor(($CLength 
/ $PageLength)) + 1; //計(jì)算頁(yè)數(shù) - $PageArray=array();
 - $Seperator = array("\n",
"\r","。","!","?",";
",",","”","’"); //分隔符號(hào) - //echo "頁(yè)數(shù):".$PageCount."< br>";
 - //echo "長(zhǎng)度:".$CLength."< br>< br>< br>";
 - //strpos() 函數(shù)返回字符串在
另一個(gè)字符串中第一次出現(xiàn)的位置 - if($CLength<$PageLength)
 - {
 - echo $content;
 - }else{
 - $PageArray[0]=0;
 - $Pos = 0;
 - $i=0;
 - //第一頁(yè)
 - for($j=0;$j< sizeof($Seperator);$j++)
 - {
 - //echo $Seperator[$j];
 - $Pos=strpos($content,
$Seperator[$j],$PageArray[$i]+1900); - while($Pos > 0 && $Pos
< ($i+1)*$PageLength &&
$Pos > $i*$PageLength ) - {
 - $PageArray[$i] = $Pos ;
 - $Pos = strpos($Pos+$PageLength,
$content,$Seperator[$j]) ; - }
 - if($PageArray[$i]>0)
 - {
 - $j = $j + sizeof($Seperator) + 1;
 - }
 - }
 - //---
 - for($i=1;$i< $PageCount-1;$i++){
 - for($j=0;$j< sizeof($Seperator);$j++)
 - {
 - //echo $Seperator[$j];
 - $Pos=strpos($content,$Seperator
[$j],$PageArray[$i-1]+1900); - while($Pos > 0 && $Pos < 
($i+1)*$PageLength && $Pos >
$i*$PageLength ) - {
 - $PageArray[$i] = $Pos ;
 - $Pos = strpos($Pos+$PageLength,
$content,$Seperator[$j]) ; - }
 - if($PageArray[$i]>0)
 - {
 - $j = $j + sizeof($Seperator) + 1;
 - }
 - }
 - }
 - //--PHP長(zhǎng)文章分頁(yè)函數(shù)最后一頁(yè)
 - $PageArray[$PageCount-1]=$CLength;
 - //$page=2;
 - if($page==1)
 - {
 - $output=substr($content,0,
$PageArray[$page-1]+2); - }
 - if($page>1 && $page< =$PageCount)
 - {
 - $output=substr($content,$PageArray
[$page-2]+2,$PageArray[$page-1]-$
PageArray[$page-2]); - $output=" (上接第".($page-1)."頁(yè))\n".$output;
 - }
 - echo str_replace("\n","< br> 
  ",$output); - //if($page==$PageCount)
 - //return $output=substr($content,
$PageArray[$page-2]+2,$PageArray[$page-1]-$PageArray[$page-2]); - if($PageCount>1)
 - {
 - echo "< br>< br>< br>< center>";
 - echo "<font color='ff0000'>".$page."< /font>/".$PageCount." 頁(yè)  ";
 - if($page>1)
 - echo "< a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page-1).">上一頁(yè)< /a> ";
 - else
 - echo "上一頁(yè) ";
 - for($i=1;$i< =$PageCount;$i++)
 - {
 - echo "< a href=$PHP_SELF?expert_id=$expert_id&page_t=".$i.">[".$i."]< /a> ";
 - }
 - if($page<$PageCount)
 - echo " < a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page+1).">下一頁(yè)< /a> ";
 - else
 - echo " 下一頁(yè) ";
 - echo "< /center>";
 - }
 - }
 - }?>
 
PHP長(zhǎng)文章分頁(yè)函數(shù)的使用 
 
- < ?php
 - $content1=''測(cè)試文字,
 
盡量長(zhǎng)一些,www.corange.cn";- $current=$_REQUEST['page_t'];
 - $result=ff_page
 
($content1,$current);- echo $result;
 - ?>
 
PHP長(zhǎng)文章分頁(yè)函數(shù)的使用與實(shí)現(xiàn)的代碼示例如上所示。
責(zé)任編輯:曹凱 
                    來源:
                    cnblogs.com
 














 
 
 



 
 
 
 