<?php$c_page="";
if(isset($_GET['page']))$c_page=$_GET['page']; 
$contstr="第一行GDSGVFGFDGFDGFDFDFDSF第2行FDSAFDSFASD。FF第3<p>
行FDSAFDSAFDSAFSDAF<p>DSFDSAF第4行DSAFDS<p>AFSDA第5行FDSAFDSFDSFD。SFFDER<p>RRR。RR。RRR。RRRR。RRRRRRRRRRRRRRRRRRRR。RRRRRRrrrrrrrrrrrrrrrrrrrrrrrr
$listbyte=70; //每页显示字节数,正常情况下可设置为5000或其他
//##寻找文章分页点----------
$contlen=strlen($contstr);
$j=0; //总页数
$listat[0]=0;
$lookAt=0;
for($i=1;$i<$contlen/$listbyte+1;$i++){
if($lookAt<$listbyte)
$lookAt=$listbyte;
if($lookAt>$contlen){
$j++;
$listat[$j]=$contlen;
break;
}
$endAt=strpos($contstr,"<p>",$lookAt); // \n
//如果余下的内容不足一页的1/5加到本页或找不到分页符则作一页处理
if($endAt>$contlen-$listbyte/5 or intval($endAt)<1){
$j++;
$listat[$j]=$contlen; ///
break;
}
else{
$j++;
$listat[$j]=$endAt;
$lookAt=$endAt+$listbyte;
}

//打印页码
$pagenum=$j;//总页
$page=intval($c_page);
if(empty($page) or $page<1 or $page>$pagenum) $page=1;
//输出当前页内容
$stag=$page-1;
$startb=$listat[$stag];
if($startb>0) //去除首个换行
$startb=$startb+1;
$cont=substr($contstr,$startb,$listat[$page]-$startb);if($page>1)
echo "";
echo $cont."<br/>";
if($pagenum>1)
for($i=1;$i<$pagenum+1;$i++){echo "<a href='?page=$i' class='hei-hong'>$i 页</a> ";}
?>

解决方案 »

  1.   

    $endAt=strpos($contstr,"<p>",$lookAt); // \n
    我以<p>为分割点,输出的文字从第二页起,前面多了一个p>的东东,如何去掉?前面的代码有误,先上传可以运行的。
    <?php$c_page="";
    if(isset($_GET['page']))$c_page=$_GET['page']; 
    $contstr="第一行GDSGVFGFDGFDGFDFDFDSF第2行FDSAFDSFASD。FF第3<p>
    行FDSAFDSAFDSAFSDAF<p>DSFDSAF第4行DSAFDS<p>AFSDA第5行FDSAFDSFDSFD。SFFDER<p>RRR。RR。RRR。RRRR。RRRRRRRRRRRRRRRRRRRR。RRRRRRrrrrrrrrrrrrrrrrrrrrrrrr<br />";$listbyte=7; //每页显示字节数,正常情况下可设置为5000或其他
    //##寻找文章分页点----------
    $contlen=strlen($contstr);
    $j=0; //总页数
    $listat[0]=0;
    $lookAt=0;
    for($i=1;$i<$contlen/$listbyte+1;$i++){
    if($lookAt<$listbyte)
    $lookAt=$listbyte;
    if($lookAt>$contlen){
    $j++;
    $listat[$j]=$contlen;
    break;
    }
    $endAt=strpos($contstr,"<p>",$lookAt); // \n
    //如果余下的内容不足一页的1/5加到本页或找不到分页符则作一页处理
    if($endAt>$contlen-$listbyte/5 or intval($endAt)<1){
    $j++;
    $listat[$j]=$contlen; ///
    break;
    }
    else{
    $j++;
    $listat[$j]=$endAt;
    $lookAt=$endAt+$listbyte;
    }

    //打印页码
    $pagenum=$j;//总页
    $page=intval($c_page);
    if(empty($page) or $page<1 or $page>$pagenum) $page=1;
    //输出当前页内容
    $stag=$page-1;
    $startb=$listat[$stag];
    if($startb>0) //去除首个换行
    $startb=$startb+1;
    $cont=substr($contstr,$startb,$listat[$page]-$startb);if($page>1)
    echo "";
    echo $cont."<br/>";
    if($pagenum>1)
    for($i=1;$i<$pagenum+1;$i++){echo "<a href='?page=$i' class='hei-hong'>$i 页</a> ";}
    ?>
      

  2.   

    将<p>用单字符替换
    ————————————————————
    我就是用<p>嘛