Solved

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

Posted on 2008-06-20
10
620 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
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 250 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 250 total points
ID: 21832248
For line 9 you would use:

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

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 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