- session_start();
- if (empty($page)) {$page=1;}
- if (isset($_GET['page'])==TRUE) {$page=$_GET['page']; }
- ?>
-
-
-
- Read Result
-
-
-
- if($page){
- $counter=file_get_contents("example.txt"); //讀取txt檔案內容到$counter
- $length=strlen($counter);
- $page_count=ceil($length/5000);
- function msubstr($str,$start,$len){
- $strlength=$start+$len;
- $tmpstr="";
- for($i=0;$i<$strlength;$i++) {
- if(ord(substr($str,$i,1))==0x0a) {
- $tmpstr.='
'; - }
- if(ord(substr($str,$i,1))>0xa0) {
- $tmpstr.=substr($str,$i,2);
- $i++;
- }
- else{
- $tmpstr.=substr($str,$i,1); }
- }
- return $tmpstr;
- }
- //------------截取中文字串---------
- $c=msubstr($counter,0,($page-1)*5000);
- $c1=msubstr($counter,0,$page*5000);
- echo substr($c1,strlen($c),strlen($c1)-strlen($c));
- }?>
|
| / 頁 |
- echo "首頁 ";
- if($page!=1){
- echo "上一頁 ";
- }
- if($page<$page_count){
- echo "下一頁 ";
- }
- echo "尾頁";
- ?>
-
|
-
複製代碼二、php讀取檔案內容 例子:
- /*
- 作者:bjf;
- 應用:讀取檔案內容;
- */
- function read_file_content($FileName)
- {
- //open file
- $fp=fopen($FileName,"r");
- $data="";
- while(!feof($fp))
- {
- //read the file
- $data.=fread($fp,4096);
- }
- //close the file
- fclose($fp);
- //delete the file
- //unlink($FileName);
- //return the content from the file
- echo $data;
- }
- read_file_content("a.html")
- ?>
-
複製代碼fread與fgets的區別 fread :以位元組位計算長度,按照指定的長度和次數讀取資料,遇到結尾或完成指定長度讀取後停止. fgets :整行讀取,遇到斷行符號換行或結尾停止.在文本方式時使用. 三、php長文章分頁顯示 例子:
-
- /**
- *Author:烏鳥heart
- *長文章分頁的代碼
- *原理:
- *利用一個數組來記錄文章每一頁(用p0、p1、p2...做手動標記)的起始位元組數,然後通過利用php函數操作這個數組去顯示分頁後的文章。分頁顯示,傳遞ptag(與tag的值一樣)值。
- *利用到的php函數:
- *1、strlen("字串") - Returns the length of the given string. - 返回字串的位元組總數。
- *2、strpos("字串","匹配字元") - Returns the numeric position of the first occurrence of needle in the haystack string. - 返回字串中出現的第一個相匹配的字元所在的位元組序數。
- *3、substr("字串","起始位置","終止位置") - substr() returns the portion of string specified by the start and length parameters. - 返回字串中指定起止位置的若干字元。
- */
- $sql = "select * from article where id = 41";//定義sql語句,返回id為41的內容
- $result = mysql_query($sql);//執行sql語句,返回結果集
- $row = mysql_fetch_array($result);//以數組的形式從屬記錄集返回
- $content = $row['content'];//把文章賦給變數$content
- $articleCounts = strlen($content);//返回$content(文章)的總位元組數
- $isTrue = true;//迴圈標記
- $tag = 0;//分頁標記、數組下標
- echo "位元組總數:".$articleCounts."
";//測試資訊
- //尋找標記“ptag”,並把其位置(所在的位元組數)賦給數組array[]------------------------------------------
- while($isTrue){
- $startAt = strpos($content,"p".$tag);//得到相應ptag的位元組序數
- if($startAt != false){ //如果有標記(傳回值不是false),則記錄位置
- $array[$tag++] = $startAt;
- }else{ //如果沒有標記,則將數組array[0]賦值'\0'
- $array[$tag] = '\0';
- $isTrue = false;
- }
- }
- //迴圈輸出標記位置--------測試資訊
- for($i = 0; $i < $tag; $i++){
- echo $array[$i]."
";
- }
- echo "------------------------------
";
- //輸出內容----
- if($array[0] == '\0'){ //判斷是否有標記
- echo $content; //沒有標記的情況,單頁顯示
- }else{ //有標記的情況,分頁顯示
- //輸出分頁內容
- if( isset($_GET['ptag']) ){ //判斷是否有ptag值傳遞,有則顯示第 ptag+1 頁,否則顯示第一頁(ptag=0)
- $ptag = $_GET['ptag']; //把ptag的值賦給變數$ptag
- if($ptag < $tag){ //判斷參數是否有誤
- echo "有值傳遞,顯示第".($ptag+1)."頁
"; //測試資訊
- echo "值為:".$ptag."
"; //測試資訊
- echo substr($content,$array[$ptag - 1] + 2,$array[$ptag] - $array[$ptag - 1] - 2);//顯示ptag+1頁的內容
- }else{echo "參數有誤";}
- }
- else{ //沒有ptag值傳遞的情況,顯示第一頁(ptag=0)
- echo "無值傳遞,顯示第1頁
"; //測試資訊
- echo substr($content,0,$array[0] - 1);//顯示第一頁的內容
- }
- }
- //迴圈顯示頁數連結----
- if($array[0] != '\0'){ //在有手動標記的情況下才顯示頁數連結
- for($i = 0;$i < $tag;$i++){
- if($ptag == $i){ //如果是本頁,則粗體顯示
- $pager .= " ".($i+1)." ";
- }else{ //不是本頁
- $pager .= " ".($i+1)." ";
- }
- }
- echo "
跳轉至第".$pager."頁"; //輸出連結
- }
- ?>
複製代碼 |