Solved

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

Posted on 2008-06-20
10
613 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now