文本文件的分页输出

自定义函数用于去除文本中的特殊字符以及空格等
<?php function unhtml($content){ //定义自定义函数的名称 $content=htmlspecialchars($content); //转换文本中的特殊字符 $content=str_replace(chr(13),"<br>",$content); //替换文本中的换行符 $content=str_replace(chr(32),"&nbsp;",$content); //替换文本中的&nbsp; $content=str_replace("[_[","<",$content); //替换文本中的大于号 $content=str_replace(")_)",">",$content); //替换文本中的小于号 $content=str_replace("|_|"," ",$content); //替换文本中的空格 return trim($content); //删除文本中首尾的空格 } //定义一个用于截取一段字符串的函数msubstr() function msubstr($str,$start,$len){ //$str指的是字符串,$start指的是字符串的起始位置,$len指的是长度。 $strlen=$start+$len; //用$strlen存储字符串的总长度(从字符串的起始位置到字符串的总长度) for($i=0;$i<$strlen;$i++){ //通过for循环语句,循环读取字符串 if(ord(substr($str,$i,1))>0xa0){ //如果字符串中首个字节的ASCII序数值大于0xa0,则表示为汉字 $tmpstr.=substr($str,$i,2); //每次取出两位字符赋给变量$tmpstr,即等于一个汉字 $i++; //变量自加1 }else{ //如果不是汉字,则每次取出一位字符赋给变量$tmpstr $tmpstr.=substr($str,$i,1); } } return $tmpstr; //输出字符串 } ?>
<?php
    if($_GET['page']==''){ //定义初始变量的值为
        $_GET['page']=1;   //变量值为1
        }
    include("function.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>文本文件的分页输出</title>
<style type="text/css">
*{
    margin:0 auto;
    }
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
a:link {
    text-decoration: none;
}
a:visited {
    text-decoration: none;
}
a:hover {
    text-decoration: none;
}
a:active {
    text-decoration: none;
}
.STYLE3 {color: #333333; font-size: 13px; }
</style>
</head>
<body>
       <table width="707" height="509" border="0" background="images/bg007.JPG">
          <tr>
            <td width="57">&nbsp;</td>
            <td width="578" height="280">&nbsp;</td>
            <td width="58">&nbsp;</td>
          </tr>
          <tr>
            <td rowspan="2">&nbsp;</td>
            <td>  <span class="STYLE3">
          
        <?php  
         //读取文本中的文件,实现超长文本的分页输出
         if($_GET['page']){
            $counter=file_get_contents("file/file.txt"); //读取文本文件
            $length=strlen(unhtml($counter));//通过自定义函数去除文本的空格,特殊字符等
            $page_count=ceil($length/1000);  //分页每页显示1000字节,ceil()四舍五入
            $c=msubstr($counter,0,($_GET['page']-1)*1000);//计算上一页的字节数
            $c1=msubstr($counter,0,$_GET['page']*1000);//计算下一页的字节数
            echo substr($c1,strlen($c),strlen($c1)-strlen($c));//获取当前的字节数
             }     
        ?>
           </td>
           <td rowspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td><span class="STYLE3">页次:<?php echo $_GET['page'];?> / <?php echo $page_count;?> 页 </span>
       分页:
       <?php
       if($_GET['page']!=1){ //判断当前是否为第一页
           echo"<a href='index.php?page=1'>首页</a>&nbsp;";//输出首页链接
           echo"<a href=index.php?page=".($_GET['page']-1).">上一页</a>&nbsp;";//输出上一页链接
           }
       if($_GET['page']<$page_count){ //判断当前是否为尾页
         echo "<a href=index.php?page=".($_GET['page']+1).">下一页</a>&nbsp;";//输出下一页链接
         echo  "<a href=index.php?page=".$page_count.">尾页</a>";//输出尾页链接  
           }
       
       
       
       ?>
          </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td height="40">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
</body>
</html>

 

posted @ 2015-10-09 15:32  骏码信息  阅读(539)  评论(0编辑  收藏  举报