PHP 抓取 txt 文件中的最后 15 行

发布于 2024-09-15 21:03:28 字数 332 浏览 4 评论 0原文

感谢您花时间阅读本文,无论内容的质量如何,我都会感激每一个回复。 :)

使用 PHP,我尝试获取文本文档 (.txt) 的最后 15 行并将该数据存储到 php 变量中。我知道这是可能的,但是当我得到最后 15 行时,是否可以保留订单?例如:

文本文档:

A
B
C

当我从最后 15 个字符抓取文本文档时,我不希望回显结果如下:

C
B
A

感谢所有帮助,我期待您的回复;谢谢。 :) 如果我没有清楚地解释任何内容和/或您希望我更详细地解释,请回复。 :)

谢谢。

Thank you for taking the time to read this and I will appreciate every single response no mater the quality of content. :)

Using PHP, I'm trying to get the last 15 lines of a text document (.txt) and store that data into a php variable. I understand that this is possible, however when I do get the last 15 lines, is it possible to retain the order? For example:

text document:

A
B
C

When I grab the text document from the last 15 characters, I don't want the echo to end up like:

C
B
A

All assistance is appreciated and I look forward to your replies; thank you. :) If I didn't explain anything clearly and/or you'd like me to explain in more detail, please reply. :)

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼前雾蒙蒙 2024-09-22 21:03:28

尝试使用 array_slice,它将返回数组的一部分。在本例中,您希望它返回数组的最后 15 行,因此:

$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);

Try using array_slice, which will return a part of an array. In this case you want it to return the last 15 lines of the array, so:

$filearray = file("filename");
$lastfifteenlines = array_slice($filearray,-15);
恏ㄋ傷疤忘ㄋ疼 2024-09-22 21:03:28

如果您不介意将整个文件加载到内存中:

$lines = array_slice(file('test.txt'), -15);
print_r($lines );

如果文件太大而无法放入内存,您可以使用循环方法:

// Read the last $num lines from stream $fp
function read_last_lines($fp, $num)
{
    $idx   = 0;

    $lines = array();
    while(($line = fgets($fp)))
    {
        $lines[$idx] = $line;
        $idx = ($idx + 1) % $num;
    }

    $p1 = array_slice($lines,    $idx);
    $p2 = array_slice($lines, 0, $idx);
    $ordered_lines = array_merge($p1, $p2);

    return $ordered_lines;
}

// Open the file and read the last 15 lines
$fp    = fopen('test.txt', 'r');
$lines = read_last_lines($fp, 15);
fclose($fp);

// Output array
print_r($lines);

如果文件少于 15 行,此方法也将起作用 - 返回一个包含任意行数的数组行在文件中。

If you don't mind loading the entire file into memory:

$lines = array_slice(file('test.txt'), -15);
print_r($lines );

If the file is too large to fit into memory you can use a circular method:

// Read the last $num lines from stream $fp
function read_last_lines($fp, $num)
{
    $idx   = 0;

    $lines = array();
    while(($line = fgets($fp)))
    {
        $lines[$idx] = $line;
        $idx = ($idx + 1) % $num;
    }

    $p1 = array_slice($lines,    $idx);
    $p2 = array_slice($lines, 0, $idx);
    $ordered_lines = array_merge($p1, $p2);

    return $ordered_lines;
}

// Open the file and read the last 15 lines
$fp    = fopen('test.txt', 'r');
$lines = read_last_lines($fp, 15);
fclose($fp);

// Output array
print_r($lines);

This method will also work if the file has less than 15 lines- returning an array with however many lines are in the file.

夜血缘 2024-09-22 21:03:28

您可以使用带有负位置的 fseek 在文件中向后查找,同时计算换行符。

我太累了,无法编写可复制/粘贴的代码,但是 fseek 手册页的注释中有一些示例非常接近您想要的。

You can use fseek with a negative position to seek backwards through the file, counting newlines as you go.

I'm too tired to write up copy/past-able code, but there are some examples in the comments to the manual page for fseek that are very close to what you want.

不疑不惑不回忆 2024-09-22 21:03:28

如果文件不大于可用内存,您可以执行以下操作:

$fArray = file("filename");
$len = sizeof($fArray);
for($i=$len -15;$i<$len ;$i++)
{
   echo $fArray[$i];
}

如果您有一个数百兆字节的文件:

$rc = fopen("file","r");
for ($i=0; $line = fgets($file) ;$i++)
{
   if ($i%15 == 0)
   {
      $last15 = array();
   }
   $last15[] = $line;
}

echo join("\n",$last15);

If the file isn't bigger than available memory you can do this:

$fArray = file("filename");
$len = sizeof($fArray);
for($i=$len -15;$i<$len ;$i++)
{
   echo $fArray[$i];
}

If you have a file that is hundreds of megabytes :

$rc = fopen("file","r");
for ($i=0; $line = fgets($file) ;$i++)
{
   if ($i%15 == 0)
   {
      $last15 = array();
   }
   $last15[] = $line;
}

echo join("\n",$last15);

心舞飞扬 2024-09-22 21:03:28

更长的数组解决方案:
array_slice(explode("\n",file_get_contents($file)),-15);

较短的数组解决方案:
array_slice(file($file),-15);

the longer array solution:
array_slice(explode("\n",file_get_contents($file)),-15);

the shorter array solution:
array_slice(file($file),-15);

长途伴 2024-09-22 21:03:28

此代码将打开文件,显示总行数,显示文件头并显示 $limit 中定义的文件的最后几行。

<?php
// open the file in read mode
$file = new SplFileObject('file.csv', 'r');

// get the total lines
$file->seek(PHP_INT_MAX);
$last_line = $file->key();
echo $last_line;
echo "<br>";

// Rewind to first line to get header
$file->rewind();

// Output first line if you need use the header to make something
echo $file->current();
echo "<br>";

// selecting the limit
$limit = 6;

// selecting the last lines using the $limit
$lines = new LimitIterator($file, $last_line - $limit, $last_line);

//print all the last 6 lines array
//print_r(iterator_to_array($lines));
//echo "<br>";

// Loop over whole file to use a single line
foreach ($lines as $line) {

    print_r($line);
    echo "<br>";

}

This code will open the file, show the total lines, show the header of file and show the last lines of file defined in $limit.

<?php
// open the file in read mode
$file = new SplFileObject('file.csv', 'r');

// get the total lines
$file->seek(PHP_INT_MAX);
$last_line = $file->key();
echo $last_line;
echo "<br>";

// Rewind to first line to get header
$file->rewind();

// Output first line if you need use the header to make something
echo $file->current();
echo "<br>";

// selecting the limit
$limit = 6;

// selecting the last lines using the $limit
$lines = new LimitIterator($file, $last_line - $limit, $last_line);

//print all the last 6 lines array
//print_r(iterator_to_array($lines));
//echo "<br>";

// Loop over whole file to use a single line
foreach ($lines as $line) {

    print_r($line);
    echo "<br>";

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文