tags:

views:

86

answers:

5

Hi there,

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.

+1  A: 
Byron Whitlock
Surely that second one only works if the number of lines in the file is a multiple of 15. If there are 20 lines (numbered 0 through 19) then you only get back the last 5 lines.
Hammerite
@hammerite you are right...
Byron Whitlock
Hi Byron Whitlock, thank you for your response, I extremely appreciate it. :) One little thing though, after reading the last 15 lines of the txt document, I want to be able to give each text it's own php variable. However, when I go to do this, it grabs the data from the last line so I can't add any variables for the above lines. Is there a way to do this? Apologies for the question, especially as it seems a little off-topic. Again, I appreciate your response. ;)
AUllah1
+1  A: 

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);
Bob Baddeley
Hi Bob Baddeley, thank you for your response as well as your efforts. I extremely appreciate it. ;) When I try to echo $lastfifteenlines, it only seems to display the text "Array" of course, I'm pretty sure that it's an error on my end and my lack of php knowledge. Again, thank you for your response and effort. ;)
AUllah1
`echo` instead `implode("\n", $lastfifteenlines)`.
Hammerite
Hi Hammerite, thanks for the response. I'm really grateful, using this code even allows me to assign variables in the correct order. Thanks for the responses as well as effort. ;)
AUllah1
+2  A: 

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.

timdev
+2  A: 

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.

David Churchill
Hey David Churchill, thank you for your response and efforts. I extremely appreciate it and can't thank you enough. ;)
AUllah1
+1  A: 

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

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

stillstanding
Hi stillstanding, I extremely appreciate your response! I'm not really too much of an expert with php, although I know it's ins and outs, I'm not too comfortable with arrays. I won't mind using arrays as long as I'll be able to assign a php variable to the array content? if possible. Anyhow, I extremely appreciate your response as well as your time and effort. ;) Thank you.
AUllah1