?
Solved

PHP script that reads the last line of a log-file

Posted on 2008-06-20
10
Medium Priority
?
631 Views
Last Modified: 2008-11-21
Hi guys.

I have this log-file which is called TEMPERATURE-2008-06-20.log and of course that filename changes when the date changes. I would like a php-script to read data in todays log-file. The content of the log-file is as follows:
20-06-2008 11:27:00,21,19
20-06-2008 11:27:05,21,25
20-06-2008 11:27:10,21,25
20-06-2008 11:27:15,21,31
20-06-2008 11:27:20,21,31
20-06-2008 11:27:25,21,25
20-06-2008 11:27:30,21,25
20-06-2008 11:27:35,21,19
20-06-2008 11:27:40,21,25
20-06-2008 11:27:45,21,25
20-06-2008 11:27:50,21,31
20-06-2008 11:27:55,21,25

I would like the script to only read the bottom line and strip the line from date and time. The only thing I want from the last line is "21", cause that is the latest meassured degrees.
Does anyone know how to build this script and write "21" into a variable?
0
Comment
Question by:Billetkontoret
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 21829505
You can try using fseek()

<?php
$filesize = filesize('log.txt');
$f = fopen('log.txt', 'r');
fseek($f, $filesize - 5);
$lastPart = fgets($f, 1024);
fclose($f);



if(preg_match('#(\d+),\d+$#', $lastPart, $m)) {
  $meassuredDegrees = $m[1];
} else {
      $meassuredDegrees = -1;
}

echo $meassuredDegrees;
?>
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21829512
Try this
$fp = fopen('23501603.php' , 'r');
$lastline = '';
do{
  $r = $lastline;
  $lastline = fgets($fp, 10000);
}while (!feof($fp));
 
echo $r;

Open in new window

0
 
LVL 2

Author Comment

by:Billetkontoret
ID: 21829534
Thanks for for comments guys!

Roonaan: The result of your script is: -1

and hernst42 yours is: 20-06-2008 11:43:45,21,25 - I need that stripped so only "21" remains.

Thank you :)
0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 2

Expert Comment

by:kszurek
ID: 21830332

<?php
$a = file_get_contents('23501603.php');
$b = explode("\n", $a);
$c = explode(",", $b[count($b)-1]);
echo $c[1];
?>

Open in new window

0
 
LVL 2

Author Comment

by:Billetkontoret
ID: 21830419
hmm, that returns me nothing. Just leaves me with a blank screen...
0
 
LVL 2

Expert Comment

by:kszurek
ID: 21830483

<?php
$fp = fopen('23501603.php' , 'r');
$lastline = '';
do{
  $r = $lastline;
  $lastline = fgets($fp, 10000);
}while (!feof($fp));
$ex = explode(",", $r);
echo $ex[1];
?>

Open in new window

0
 
LVL 2

Accepted Solution

by:
kszurek earned 1000 total points
ID: 21830501
Sorry, try:
<?php
$fp = fopen('23501603.php' , 'r');
$lastline = '';
do{
  $r = $lastline;
  $lastline = fgets($fp, 10000);
}while (!feof($fp));
$ex = explode(",", $lastline);
echo $ex[0];
?>

Open in new window

0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 1000 total points
ID: 21832248
For line 9 you would use:

echo $ex[1]; rather than $ex[0];
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question