php method to implement txt file paging: 1. Create a PHP sample file; 2. Process Chinese characters through "function m_substr($str, $start, $length){...}" and Just achieve the paging effect.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
How to realize paging of txt files in php?
php can simply realize the paging function of text files
<!DOCTYPE> <html> <head> <meta http-equiv="Content-type" content="text/html";charset="gb2312"> <title>Paging</title> <style> a{ padding:20px; } </style> </head> <body> <?php // 中文字符处理 function m_substr($str, $start, $length){ $str_length = $start + $length; // 获取截取总长度 $tmp_str = ""; for($i=0;$i<$str_length;$i++){ if(ord(substr($str, $i, 1)) == 0x0a){ $tmp_str .= "<br>"; } if(ord(substr($str, $i, 1))>0xa0){ $tmp_str .= substr($str, $i, 2); $i++; }else{ $tmp_str .= substr($str, $i, 1); } } return $tmp_str; } // 传参处理 if(isset($_GET['page'])){ $page = $_GET['page']; }else{ $page = 1; } $counter = file_get_contents("example.txt"); $length = strlen($counter); $page_count = ceil($length/400); $pre_str = m_substr($counter, 0, ($page-1)*400); $now_str = m_substr($counter, 0, $page*400); echo substr($now_str, strlen($pre_str), strlen($now_str)-strlen($pre_str)); echo "<br><br>"; echo "当前页".$page."/".$page_count; echo "<a href='index.php?page=1'>Index</a>"; if($page>1){ echo "<a href='index.php?page=".($page-1)."'>Pre</a>"; } if($page<$page_count){ echo "<a href=index.php?page=".($page+1).">Next</a>"; } echo "<a href=index.php?page=$page_count>End</a>"; ?> </body> </html>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement paging of txt files in php. For more information, please follow other related articles on the PHP Chinese website!