Solved

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

Posted on 2008-06-20
10
623 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

740 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