刘华世的官方博客

php对文本文件进行分页功能简单实现

php对文本文件进行分页功能简单实现

<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html";charset="gb2312">
    <title>Paging</title>
    <style>
        a{
            padding:20px;
        }
    </style>
</head>
<body>

<?php
// 中文字符处理
function m_substr($str, $start, $length){
    $str_length = $start + $length; // 获取截取总长度
    $tmp_str = "";
    for($i=0;$i<$str_length;$i++){
        if(ord(substr($str, $i, 1)) == 0x0a){
            $tmp_str .= "<br>";
        }
        if(ord(substr($str, $i, 1))>0xa0){
            $tmp_str .= substr($str, $i, 2);
            $i++;
        }else{
            $tmp_str .= substr($str, $i, 1);
        }
    }
    return $tmp_str;
}
// 传参处理
if(isset($_GET['page'])){
    $page = $_GET['page'];
}else{
    $page = 1;
}

$counter = file_get_contents("example.txt");
$length = strlen($counter);
$page_count = ceil($length/400);
$pre_str = m_substr($counter, 0, ($page-1)*400);
$now_str = m_substr($counter, 0, $page*400);
echo substr($now_str, strlen($pre_str), strlen($now_str)-strlen($pre_str));

echo "<br><br>";
echo "当前页".$page."/".$page_count;
echo "<a href='index.php?page=1'>Index</a>";
if($page>1){
    echo "<a href='index.php?page=".($page-1)."'>Pre</a>";
}
if($page<$page_count){
    echo "<a href=index.php?page=".($page+1).">Next</a>";
}

echo "<a href=index.php?page=$page_count>End</a>";

?>

</body>
</html>

posted @ 2015-09-10 15:22  刘华世的博客  阅读(1660)  评论(0编辑  收藏  举报
刘华世的官方博客