-
- 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")
- ?>
-
复制代码
The difference between fread and fgets
fread: Calculate the length in bytes, read data according to the specified length and number of times, and stop after encountering the end or completing the specified length of reading.
fgets: Read the entire line and stop when encountering a carriage return or line feed or the end. Used in text mode.
3. Paging long articles in PHP
Example:
-
-
- /**
- *Author: Wuniao heart
- *Code for paging long articles
- *Principle:
- *Use an array to record the starting number of bytes of each page of the article (manually marked with p0, p1, p2...), Then use the php function to operate this array to display the paginated articles. For paging display, pass the ptag (same as the value of tag) value.
- *PHP functions used:
- *1. strlen("string") - Returns the length of the given string. - Returns the total number of bytes in the string.
- *2, strpos("string","matching character") - Returns the numeric position of the first occurrence of needle in the haystack string. - Returns the byte of the first matching character that appears in the string Ordinal.
- *3, substr("string","start position","end position") - substr() returns the portion of string specified by the start and length parameters. - Returns several characters at the specified start and end positions in the string .
- */
- $sql = "select * from article where id = 41";//Define the sql statement and return the content with ID 41
- $result = mysql_query($sql);//Execute the sql statement and return the result set
- $row = mysql_fetch_array($result);//In the form of an array Return from the record set
- $content = $row['content'];//Assign the article to the variable $content
- $articleCounts = strlen($content);//Return the total number of bytes of $content (article)
- $ isTrue = true;//Loop tag
- $tag = 0;//Paging tag, array subscript
- echo "Total number of bytes: ".$articleCounts."
";//Test information
- //Look for tags "ptag" and assign its position (number of bytes) to the array array[]-------------------------------- -------------
- while($isTrue){
- $startAt = strpos($content,"p".$tag);//Get the byte order of the corresponding ptag
- if($ startAt != false){ //If there is a tag (the return value is not false), record the position
- $array[$tag++] = $startAt;
- }else{ //If there is no tag, assign the array array[0] '
-
-
-
-
-
-
-
|