Home >Backend Development >PHP Problem >How to implement paging of txt files in php

How to implement paging of txt files in php

藏色散人
藏色散人Original
2021-12-13 10:20:382278browse

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.

How to implement paging of txt files in php

#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[&#39;page&#39;])){
    $page = $_GET[&#39;page&#39;];
}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=&#39;index.php?page=1&#39;>Index</a>";
if($page>1){
    echo "<a href=&#39;index.php?page=".($page-1)."&#39;>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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn