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>