分页读取文本文件

课后整理 2021-1-7

在遍历文件中的内容时,由于文件内容很多,最理想的方法就是分页读取文本文件中的内容。本例将介绍如何分页读取文本文件中的数据,运行结果如图11.21所示。

图11.21 分页读取文本文件

【操作步骤】

第1步,创建function.php文件,编写自定义函数msubstr(),完成对文本文件的截取操作。

<?php
//定义一个用于截取一段字符串的函数msubstr()
function  msubstr($str,$start,$len){ //$str指的是字符串,$start指的是字符串的起始位置,$len指的是长度。
$strlen=$start+$len;//用$strlen存储字符串的总长度(从字符串的起始位置到字符串的总长度)
$tmpstr = "";
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;                //输出字符串
}
?>

完成超长文本的分页输出需要3方面的技术:第一个方面,自定义函数。通过自定义函数读取文本文件,可以避免中文字符串出现乱码;第二个方面,字符串函数。通过strlen()函数计算字符串的长度,通过substr()函数对字符串进行截取;第三个方面,文件系统函数。通过file_get_contents()函数读取文本文件中的数据。

第2步,创建index.php文件,首先通过文件系统函数file_get_contents()读取整个文件的内容,然后调用自定义函数和字符串函数完成对文件的截取操作,实现截取后内容的分页输出。

<div  id="synopsis">
<!--创建div标签,用于获取js文件中返回的分页结果-->
<?php         
 include("function.php");    
//读取超长文本中的数据,实现超长文本中数据的分页显示
if($_GET['page']){
    $counter=file_get_contents("data.txt");
    $length=strlen($counter);
    $page_count=ceil($length/800);
    $c=msubstr($counter,0,($_GET['page']-1)*800);
    $c1=msubstr($counter,0,$_GET['page']*800);
    echo  "<p>".substr($c1,strlen($c),strlen($c1)-strlen($c))."</p>"; 
}
?>
<!--设置超长文本分页显示的超级连接--> 
<p>第<?php echo $_GET['page'];?>页&nbsp;&nbsp;共<?php echo $page_count;?>页</p>
<p>
<?php
if($_GET['page']!=1){ 
?>
    <!--调用no_refurbish_pagination函数,实现无刷新的分页输出-->    
    <a href="#" onclick='return  no_refurbish_pagination("index_ok.php?page=1")'>首页</a>&nbsp; <a  href="#" onclick='return  no_refurbish_pagination("index_ok.php?page=<?php echo  $_GET['page']-1;?>")'>上一页</a>
<?php
}
if($_GET['page']<$page_count){
?>
    <a href="#"  onClick="return no_refurbish_pagination('index_ok.php?page=<?php echo  $_GET['page']+1;?>')">下一页</a> <a href="#" onclick='return  no_refurbish_pagination("index_ok.php?page=<?php echo  $page_count;?>")'>尾页</a>
<?php
}
?>
</p>
</div>

第3步,创建index_ok.php文件,实现数据的无刷新分页。index_ok.php中的内容与index.php中<div id="synopsis">包含的内容相同。

第4步,创建Javascript脚本文件discuss.js。通过Ajax实现无刷新的操作。

var xmlHttp = false;
try {
       xmlHttp = new  ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
       try {
            xmlHttp = new  ActiveXObject("Microsoft.XMLHTTP");
       } catch (e2) {}
}
if (!xmlHttp && typeof  XMLHttpRequest != "undefined") {
    try{
           xmlHttp = new  XMLHttpRequest();
    }catch(e3){ xmlHttp = false;}
}
//使用XMLHttpRequest对象创建异步HTTP请求
function  no_refurbish_pagination(url){ //创建自定义函数,获取传递的参数
    xmlHttp.open('get',url,true); //根据传递的参数,通过get方法,执行另外一个实现分页功能的文件
    xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readystate == 4 &&  xmlHttp.status == 200){ //将结果返回到div标签synopsis中
            document.getElementById("synopsis").innerHTML  = xmlHttp.responseText;
        }
    }
    xmlHttp.send(null);
}

在本例中不但实现了文本文件内容的分页输出,而且是无刷新分页输出。具体操作通过Javascript脚本文件discuss.js和index_ok.php完成。