PHP 长文章分页函数 带使用方法,不会分割段落,翻页在底部
程序员文章站
2022-06-20 14:37:22
复制代码 代码如下:
<?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 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;
?>
上一篇: Python with标签使用方法解析