-2

I am trying to pull a specific bit of data out of the last line of a text file to be able to display it on it's own. For example I want to pull the rainfall data out of the following file:

#date #time  #blah #rainfall  #blah   #blah
200813 1234   1234    0.5      1234    1234
200813 1235   1234    1.2      1234    1234
200813 1236   1234    3.5      1234    1234
200813 1237   1234    0.2      1234    1234
200813 1238   1234    0.1      1234    1234

And I want to use the data in this way on a web page:

Rainfall predicted now: 0.1mm

So all I need is that 0.1 figure out of the last line. As the file is remote and new lines are added at the bottom of the file, I only ever need the last line.

Can someone please help, I have been wracking my brains over this for days.

  • 3
    Please do use the search before making a new question, duplicates [PHP - Returning the last line in a file?](http://stackoverflow.com/questions/1062716/php-returning-the-last-line-in-a-file) there is also this one [Read last line from file](http://stackoverflow.com/questions/1510141/read-last-line-from-file) – Prix Aug 20 '13 at 16:44
  • Please do read my question thouroughly before suggested topics that are distantly related. I searched SO already, none of them tell me how to select text from a position in a line to a position in a line. As the data will always change I am assuming I have to select a section of the line. I cannot match the data with an if statement for example, as the data is never the same. – ArnoldJRimmer Aug 20 '13 at 16:51
  • Then please do read both links I have pointed above which answers your question on how to get the last line. once you know how to get the last line is a matter of exploding the variable. – Prix Aug 20 '13 at 16:53
  • @user2700686: Please do a more fair judegment here. You are asking two things where normally it is epxected that you ask one thing. So please don't complain when for both the two very common things only for one duplicates are suggested. For the other common one - how to parse a string - duplicates exist as well. So better keep your question more to a certain point otherwise comments and answers go off. – hakre Aug 20 '13 at 16:58
  • It was not a duplicate. Closely related, perhaps. Duplicate, no. I got my answer here, why didn't I get it in the other threads? I can see your point however and explains why you are trigger happy to pull the duplicate trigger on your duplicate gun, I can totally understand it being a mod of many sites myself, but in this instance, your wrong. I have my answer now, thanks to Hakre. So thanks and goodbye :) – ArnoldJRimmer Aug 20 '13 at 17:49
  • @user2700686 yep you got your answer out of the kindness of someone who also voted to close your question, because you're too lazy to put the effort into it. Which if you had, you would have found the answer with 2 small searches, but you're right we can't expect something from people like you **that just want to be spoonfed and is not here to learn.** – Prix Aug 20 '13 at 20:50

3 Answers3

2
$file = file('path/to/file');
$lastLine = end($file);

Should do what you need.

Or, if you are a fan of one liners:-

$lastLine = end(file('path/to/file'));

This will output your expected rainfall assuming that your file is called 'data.txt':-

printf('Rainfall expected %smm', array_values(array_filter(explode(' ', end(file('data.txt')))))[3]);

See file() and end()

vascowhite
  • 18,120
  • 9
  • 61
  • 77
0

It's normally done with a one-liner if the file is not too large, or close to one:

vprintf(
    'Rainfall prediced now: %4$smm'
    , explode(' ', end((
        file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
    )))
);

If the input format is more complex, also use sscanf or preg_match to parse the last line.

Edit As you write the file is small, you can also load it into a string (file_get_contents) and parse that string from behind:

$buffer = '#date #time  #blah #rainfall  #blah   #blah
200813 1234   1234    0.5      1234    1234
200813 1235   1234    1.2      1234    1234
200813 1236   1234    3.5      1234    1234
200813 1237   1234    0.2      1234    1234
200813 1238   1234    0.1      1234    1234';

preg_match('/([^ ]+)\s+\d+\s+\d+\R?$/', $buffer, $matches)
    && vprintf('Rainfall prediced now: %2$smm', $matches);

// prints "Rainfall prediced now: 0.1mm"
hakre
  • 193,403
  • 52
  • 435
  • 836
0

If you're using an Unix based system, consider using the tail command.

$file = escapeshellarg($file); 
$line = `tail -n 1 $file`;

The following will work, too:

$fp = fopen('file.txt', 'r');
$pos = -1; $line = ''; $c = '';
do {
    $line = $c . $line;
    fseek($fp, $pos--, SEEK_END);
    $c = fgetc($fp);
} while ($c != PHP_EOL);

echo $line; //last line

fclose($fp); 

As mentioned here, this does not load the whole file into the memory, and is fast.

Hope this helps!

Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • Hi, I tried the 2nd code and got this "stream does not support seeking in /public_html/.." I will try the first code now. – ArnoldJRimmer Aug 20 '13 at 17:29