表>
> / > 頁 span>
表> 身體>
複製程式碼
二、php讀取檔案內容
例:
-
-
/*
- 作者:bjf;
- 應用:讀取檔案內容;
- */
- function read_file_content($FileName)
- {
- //開啟檔案
- $fp=fopen($FileName,"r");
- $data="";
- while(!feof ($fp))
- {
- //讀取檔案
- $data.=fread($fp,4096);
- }
- //關閉檔案
- fclose($fp );
- //刪除檔案
- //unlink($FileName);
- //傳回檔案內容
- 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]賦值'
-
-
-
-
-
-
-
-
-
session_start();
- if (empty($page)) {$page=1;}
- if (isset( $_GET['page'])==TRUE) {$page=$_GET['page']; }
- ? >
-
-
-
>
-
讀取結果
-
-
- 風格>
- 頭>
-
-
-
|
if($page){
$counter=file_get_contents("example.txt"); //讀取txt檔案內容到$counter
$length= strlen($counter);
$page_count=ceil($length/5000);
函數msubstr($str,$start,$len){
$strlength=$start $len;
$tmpstr="";
for($i=0;$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); }
}
回傳$ 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 echo "下一頁> 」;
}
echo "尾頁 a>"; ? >
|