這篇文章主要介紹了PHP實現的超長文本分頁顯示功能,涉及php針對字串的計算、截取、遍曆等相關操作技巧,可實現針對超長文本的分頁顯示功能,需要的朋友可以參考下
本文執行個體講述了PHP實現的超長文本分頁顯示功能。分享給大家供大家參考,具體如下:
一、代碼
1、index.php
<?php if ($_GET=="") {$_GET=1;}; include("function.php");?><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>超長文本的分頁顯示</title></head><style type="text/css"><!--body { margin-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px;}a:link { text-decoration: none;}a:visited { text-decoration: none;}a:hover { text-decoration: none;}a:active { text-decoration: none;}.STYLE3 {color: #333333; font-size: 13px; }--></style><body> <table width="876" height="638" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="343" height="159"> </td> <td width="489"> </td> <td width="44"> </td> </tr> <tr> <td height="245"> </td> <td align="center" valign="top"><table width="480" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="22" colspan="2"> <span class="STYLE3"> <?php//讀取超長文本中的資料,實現超長文本中資料的分頁顯示 if($_GET){ $counter=file_get_contents("file/file.txt"); $length=strlen($counter); $page_count=ceil($length/950); $c=msubstr($counter,0,($_GET-1)*950); $c1=msubstr($counter,0,$_GET*950); echo substr($c1,strlen($c),strlen($c1)-strlen($c)); }?> </span> </td> </tr> <tr> <td width="202" height="22"><span class="STYLE3">頁次:<?php echo $_GET;?> / <?php echo $page_count;?> 頁 </span></td> <td width="278"><span class="STYLE3">分頁: <?php if($_GET!=1){ echo "<a href=index.php?page=1>首頁</a> "; echo "<a href=index.php?page=".($_GET-1).">上一頁</a> "; } if($_GET<$page_count){ echo "<a href=index.php?page=".($_GET+1).">下一頁</a> "; echo "<a href=index.php?page=".$page_count.">尾頁</a>"; } ?> </span></td> </tr></table></td> <td> </td> </tr> <tr> <td height="234"> </td> <td> </td> <td> </td> </tr></table></body></html>
2、function.php
<?php//定義一個用於截取一段字串的函數msubstr() function msubstr($str,$start,$len){ //$str指的是字串,$start指的是字串的起始位置,$len指的是長度。 $strlen=$start+$len; //用$strlen儲存字串的總長度(從字串的起始位置到字串的總長度) for($i=0;$i<$strlen;$i++){ //通過for迴圈語句,迴圈讀取字串 if(ord(substr($str,$i,1))>0xa0){ //如果字串中首個位元組的ASCII序數值大於0xa0,則表示為漢字 $tmpstr.=substr($str,$i,2); //每次取出兩位字元賦給變數$tmpstr,即等於一個漢字 $i++; //變數自加1 }else{ //如果不是漢字,則每次取出一位字元賦給變數$tmpstr $tmpstr.=substr($str,$i,1); } } return $tmpstr; //輸出字串 }?>
二、運行結果