Jump to content

[SOLVED] getting data from a log file


admalledd

Recommended Posts

still rather new to all this coding in php, but giving it a try...

 

I need a way to get some text in my log file to be placed in strings,

things i need: time received, (optional) start string, the ten numbers ,the end string

a bit of log file looks like this:

 

Received: 8/4/2008  2:54:16 AM      - start

Received: 8/4/2008  2:54:16 AM      - 1, 2, 3, 4, 5

Received: 8/4/2008  2:54:16 AM      - 6, 7, 8, 9, 10

Received: 8/4/2008  2:54:16 AM      - end --(ending condition)

 

where 1,2,3,4,5 ect are numbers from 0 to 1024

note: log file updates every .025 to 0.25 seconds.

 

what I have so far for code is:

<?php
$file = file_get_contents("log.txt");
$needle = 'start''
$pos = strrpos($file, $needle);
//magic
$num1 = ????
$num2 = ????
$num3 = ????
$num4 = ????
$num5 = ????
$num6 = ????
$num7 = ????
$num8 = ????
$num9 = ????
$num10 = ????
?>

 

 

Been stuck on this for couple days now, thought I might just ask now.

Any tips tricks or key words to Google will be helpful

thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/118621-solved-getting-data-from-a-log-file/
Share on other sites

Wait I am confused what you are trying to do.

You want each line to be held in a variable?

 

if so do this:

 

$file = file_get_contents("log.txt");

foreach ($file as $array){

echo "num".$array. = $array;
}

 

Then you should be able to manage them like an array.

 

echo $array[1],2,3 etc

sorry, looked at the time, seems I have gone without sleep again...

 

kind of, but that seems to only put the line into a array, that's close but, i need to get the start and end data too (preferably in separate variables)

what i was thinking as far as variabls/strings go...

 

$log_start = //TRUE or FALSE

$log_time[1 thru 10] = //time both num log lines recieved

$log_numbers[1 thru 10] = // actual numbers given in log

$log_end = //last line of log usual

 

 

also i get an error on the foreach() line when I run it, any ideas?

 

ps: be back after tea. mmm... tea...

sorry, looked at the time, seems I have gone without sleep again...

 

kind of, but that seems to only put the line into a array, that's close but, i need to get the start and end data too (preferably in separate variables)

what i was thinking as far as variabls/strings go...

 

$log_start = //TRUE or FALSE

$log_time[1 thru 10] = //time both num log lines recieved

$log_numbers[1 thru 10] = // actual numbers given in log

$log_end = //last line of log usual

 

 

also i get an error on the foreach() line when I run it, any ideas?

 

ps: be back after tea. mmm... tea...

Use file() instead of file_get_contents()

 

$lines = file('log.txt');

foreach ($lines as $line)
{
    echo $line;
}

 

To get the first and last line from the $lines array, you'd use array_shift and array_pop

I don't understand how $log_time can be an array of ten different times when there are only 4 total times listed, so I just made it the one time that the numbers occur.

$test = file_get_contents("log.txt");
preg_match("/Received:\s*(.+?)-\s*start\s*Received:\s*(.+?)-\s*(.+?)\s*Received:\s*.+?-\s*(.+?)\s*Received:\s*(.+?)-\s*end/", $test, $matches);
$log_start = $matches[1];
$log_time = $matches[2];
$log_numbers = array_merge(explode(",", $matches[3]), explode(",", $matches[4]));
$log_end = $matches[5];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.