Rumah > pembangunan bahagian belakang > tutorial php > php对文本文件开展分页功能简单实现

php对文本文件开展分页功能简单实现

WBOY
Lepaskan: 2016-06-13 12:24:24
asal
842 orang telah melayarinya

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

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

    <meta http-equiv="Content-type" content="text/html">    <title>Paging</title>    <style><span style="color: #000000;">        a{            padding:<span style="color: #000000;">20px;        }    </style><span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> 中文字符处理</span><span style="color: #0000ff;">function</span> m_substr(<span style="color: #800080;">$str</span>, <span style="color: #800080;">$start</span>, <span style="color: #800080;">$length</span><span style="color: #000000;">){    </span><span style="color: #800080;">$str_length</span> = <span style="color: #800080;">$start</span> + <span style="color: #800080;">$length</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> 获取截取总长度</span>    <span style="color: #800080;">$tmp_str</span> = ""<span style="color: #000000;">;    </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$str_length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$str</span>, <span style="color: #800080;">$i</span>, 1)) == 0x0a<span style="color: #000000;">){            </span><span style="color: #800080;">$tmp_str</span> .= "<br>"<span style="color: #000000;">;        }        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">ord</span>(<span style="color: #008080;">substr</span>(<span style="color: #800080;">$str</span>, <span style="color: #800080;">$i</span>, 1))>0xa0<span style="color: #000000;">){            </span><span style="color: #800080;">$tmp_str</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$str</span>, <span style="color: #800080;">$i</span>, 2<span style="color: #000000;">);            </span><span style="color: #800080;">$i</span>++<span style="color: #000000;">;        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #800080;">$tmp_str</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$str</span>, <span style="color: #800080;">$i</span>, 1<span style="color: #000000;">);        }    }    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$tmp_str</span><span style="color: #000000;">;}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 传参处理</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">])){    </span><span style="color: #800080;">$page</span> = <span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">];}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{    </span><span style="color: #800080;">$page</span> = 1<span style="color: #000000;">;}</span><span style="color: #800080;">$counter</span> = <span style="color: #008080;">file_get_contents</span>("example.txt"<span style="color: #000000;">);</span><span style="color: #800080;">$length</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$counter</span><span style="color: #000000;">);</span><span style="color: #800080;">$page_count</span> = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$length</span>/400<span style="color: #000000;">);</span><span style="color: #800080;">$pre_str</span> = m_substr(<span style="color: #800080;">$counter</span>, 0, (<span style="color: #800080;">$page</span>-1)*400<span style="color: #000000;">);</span><span style="color: #800080;">$now_str</span> = m_substr(<span style="color: #800080;">$counter</span>, 0, <span style="color: #800080;">$page</span>*400<span style="color: #000000;">);</span><span style="color: #0000ff;">echo</span> <span style="color: #008080;">substr</span>(<span style="color: #800080;">$now_str</span>, <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$pre_str</span>), <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$now_str</span>)-<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$pre_str</span><span style="color: #000000;">));</span><span style="color: #0000ff;">echo</span> "<br><br>"<span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "当前页".<span style="color: #800080;">$page</span>."/".<span style="color: #800080;">$page_count</span><span style="color: #000000;">;</span><span style="color: #0000ff;">echo</span> "<a href="index.php?page=1">Index</a>"<span style="color: #000000;">;</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span>>1<span style="color: #000000;">){    </span><span style="color: #0000ff;">echo</span> "<a href="index.php?page=%22.(<span%20style=%22color:%20#800080;%22>%24page</span>-1).%22">Pre</a>"<span style="color: #000000;">;}</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$page</span>$page_count<span style="color: #000000;">){    </span><span style="color: #0000ff;">echo</span> "<a href="index.php?page=%22.(<span" style="color: #800080;">$page+1).">Next</a>"<span style="color: #000000;">;}</span><span style="color: #0000ff;">echo</span> "<a href="index.php?page=<span" style="color: #800080;">$page_count>End</a>"<span style="color: #000000;">;</span>?>
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan