首页 > php教程 > php手册 > 正文

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

WBOY
发布: 2016-06-13 08:54:34
原创
1233 人浏览过

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>
登录后复制

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板