Home >Backend Development >PHP Tutorial >How to read txt text file with PHP and display it in pages, _PHP tutorial
The example in this article describes how PHP reads a txt text file and displays it in pages. Share it with everyone for your reference. The specific implementation method is as follows:
Copy code The code is as follows:
Session_start();
If (empty($page)) {$page=1;}
If (isset($_GET['page'])==TRUE) {$page=$_GET['page']; }
?>
if($page){ $counter=file_get_contents("example.txt"); //Read the contents of the txt file to $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; } //------------Intercept Chinese string--------- $c=msubstr($counter,0,($page-1)*5000); $c1=msubstr($counter,0,$page*5000); echo substr($c1,strlen($c),strlen($c1)-strlen($c)); }?> |
/ Page | echo "Homepage "; if($page!=1){ echo "Previous page "; } if($page<$page_count){ echo "Next page "; } echo "Last page"; ?> |
I hope this article will be helpful to everyone’s PHP programming design.