首頁 >php教程 >php手册 >php对文本文件进行分页功能简单实现,

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

WBOY
WBOY原創
2016-06-13 08:54:341256瀏覽

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

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

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

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

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

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

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

</span>?>

</body>
</html>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn