PHP 长文章分页函数 带使用方法,不会分割段落,翻页在底部

yipeiwu_com5年前PHP代码库
复制代码 代码如下:

<?php function ff_page($content,$page)
{
global $expert_id;
$PageLength = 2000; //每页字数
$CLength = strlen($content);
$PageCount = floor(($CLength / $PageLength)) + 1; //计算页数
$PageArray=array();
$Seperator = array("\n","\r","。","!","?",";",",","”","'"); //分隔符号

//echo "页数:".$PageCount."<br>";
//echo "长度:".$CLength."<br><br><br>";
//strpos() 函数返回字符串在另一个字符串中第一次出现的位置
if($CLength<$PageLength)
{
echo $content;

}else{

$PageArray[0]=0;
$Pos = 0;
$i=0;
//第一页
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;
}
}
}


//--最后一页
$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)."页)\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." 页  ";
if($page>1)
echo "<a href=$PHP_SELF?expert_id=$expert_id&page_t=".($page-1).">上一页</a> ";
else
echo "上一页 ";

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).">下一页</a> ";
else
echo " 下一页 ";

echo "</center>";
}
}

}?>

使用
复制代码 代码如下:

<?php
$content1=''测试文字,尽量长一些,www.jb51.net";
$current=$_REQUEST['page_t'];
$result=ff_page($content1,$current);
echo $result;
?>

相关文章

php foreach、while性能比较

foreach是对数组副本进行操作(通过拷贝数组),而while则通过移动数组内部指标进行操作,一般逻辑下认为,while应该比foreach快(因为foreach在开始执行的时候首先把...

php数组键值用法实例分析

本文实例讲述了php数组键值用法。分享给大家供大家参考。具体分析如下: 先看一个数组: <?php $switching = array( 10, // key =...

PHP实现格式化文件数据大小显示的方法

本文实例讲述了PHP实现格式化文件数据大小显示的方法。分享给大家供大家参考。具体分析如下: 有时候我们需要在网页上显示某个文件的大小,或者是其它数据的大小数字。 这个数字往往从跨度很大,...

PHP的APC模块实现上传进度条

PHP的APC模块实现上传进度条

APC模块,它的全称是Alternative PHP Cache。APC可以将所有PHP代码会被缓存起来, 另外它可提供一定的内存缓存功能.但是这个功能并不是十分完美,有报告说如果频繁使...

FastCGI 进程意外退出造成500错误

FastCGI 进程意外退出造成500错误

在一台新服务器上,安装新网站,之前只放至了一个网站。是服务器商配置好的,非集成环境。 添加了一个新站,路径都制定好了,但是在访问时出现了500错误。提示貌似是php的问题,但是之前的网站...