时间:2023-02-23加入收藏
PS:上面的案例如果要用到项目中,需要自己改造一下!//因为处理的是中文,所以要使用 mb_substr 函数
function m_substr($str, $start, $length){
$str_length = $start + $length;
$tmp_str = "";
for($i=0;$i<$str_length;$i++){
if(ord(mb_substr($str, $i, 1)) == 0x0a){
$tmp_str .= "";
}
if(ord(mb_substr($str, $i, 1)) > 0xa0){
$tmp_str .= mb_substr($str, $i, 2);
$i++;
}else{
$tmp_str .= mb_substr($str, $i, 1);
}
}
return $tmp_str;
}
function str_strLen($string)
{
if (function_exists('grapheme_strlen')) {
return grapheme_strlen($string);
}
if (function_exists('mb_strlen') && function_exists('mb_internal_encoding')) {
mb_internal_encoding('UTF-8');
return mb_strlen($string);
}
if (function_exists('iconv_strlen') && function_exists('iconv_set_encoding')) {
call_user_func('iconv_set_encoding', 'internal_encoding', "UTF-8");
call_user_func('iconv_set_encoding', 'output_encoding', "UTF-8");
return iconv_strlen($string);
}
return strlen($string);
}
//获取提交的页号
if(isset($_GET['page'])){
$page = (int)$_GET['page'];
}else{
$page = 2;
}
$page_txtlength = 800;
$content = file_get_contents("txt.txt"); //加载文本内容
$length = str_strLen($content); //获取文本的长度
$page_count = ceil($length/$page_txtlength);
$pre_str = m_substr($content, 0, ($page-1)*$page_txtlength);
$now_str = m_substr($content, 0, $page*$page_txtlength);
echo substr($now_str, strlen($pre_str), strlen($now_str)-strlen($pre_str));
echo "<hr/>";
echo '当前页'.$page.'/'.$page_count;
echo '<p>';
echo '<a href="e.php?page=1">首页</a>';
echo ' ';
if($page > 1){
echo '<a href="e.php?page='.($page-1).'">上一页</a>';
}
echo ' ';
if($page < $page_count){
echo '<a href="e.php?page='.($page+1).'">下一页</a>';
}
echo ' ';
echo '<a href="e.php?page='.$page_count.'">未页</a>';
echo '</p>';