PHP:大文本文件的分页

时间:2018-09-03 06:36:45

标签: php html pagination text-files

我的网站上有一个文本文件,该文件实际上占据了很多垂直空间,并且必须向下滚动很多才能阅读。我想按每页划分它。例如,每300个字符或每10行就有一个新页面。有可能的?谢谢

3 个答案:

答案 0 :(得分:0)

一种解决方法是将文本文件从大字符串转换为数组,然后根据要显示的行数对数组进行切片

如果我们假设一次显示50行,并且脚本通过GET接收页面:

$linesperpage = 50;
$page = (int) $_GET['page'];

$textfile = '...';
$linescount = 0;
$linescount = substr_count( $textfile , PHP_EOL );

$lines = array();
$lines = explode(PHP_EOL, $textfile);

$linestodisplay = '';

$linestodisplay = implode(PHP_EOL, array_slice($lines, $linesperpage * $page -1, $linesperpage * ($page+1) -1));

echo $linestodisplay;

答案 1 :(得分:0)

确定每300个字符为一个字符。

// file path to read the text file
$filePath = "textfile.txt";
// now character per page we need 
$charsPerPage = 300;
// now get the page number after click and default is 1
// taking you are passing the pagenumber in url like ?page-number=?
$pageNumber = isset($_GET["page-number"])?$_GET["page-number"]:1;
// Determine Starting character index and ending character index
// starting character index for first page character index should be 0 and ending character index is 300
$startingIndex = $charsPerpage*($pageNumber-1);
// Now read the text file
$textFile = fopen($filePath, "r") or die("Unable to open file!");
// read all text of text file
$textContent = fread($myfile,filesize($filePath));
// now it's time to fetch page conetent
$pageContent = substr($textContent,$startingIndex,$charsPerPage);
echo $pageContent;

答案 2 :(得分:0)

嗨,我正在尝试两种解决方案,但是我很难适应我的代码。无论如何,这并不是很紧急,我稍后会更加冷静地再次尝试您的解决方案。抱歉,如果我之前没有回答,但是之前和之后的假期中,请让我离开计算机。