Heim >Backend-Entwicklung >PHP-Tutorial >php对文本文件开展分页功能简单实现

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:24:24865Durchsuche

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>?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn