Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-20
10
Medium Priority
?
639 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
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 …
Suggested Courses

885 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