Help with parsing a file please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wilded1
Forum Newbie
Posts: 9
Joined: Sun Feb 21, 2010 11:49 am

Help with parsing a file please.

Post by wilded1 »

Hi all.
I'm Dave and I am new to the forum.
I was wondering if anyone could help?
I have a need to parse a log file and I am lost. I wonder if there is some kind soul that could help me with this task.

I have a log file:

Code: Select all

022110 at 15:39:11:Is Postgres running?
022110 at 15:39:11:Yes, Postgres is running
022110 at 15:39:11:Reading file *****
022110 at 15:39:11:OWNER=[*****   ]
022110 at 15:39:11:EXTERNAL_IF=[eth0]
022110 at 15:39:11:GW_PORT=[40000]
022110 at 15:39:11:INTERNAL_IF=[eth1]
022110 at 15:39:11:G2_INTERNAL_PORT=[20000]
022110 at 15:39:11:TO_RPTR_IP=[*******]
022110 at 15:39:11:RPTR_PORT=[20000]
022110 at 15:39:11:PORT_RENEWALS=[12346]
022110 at 15:39:11:QSO_DETAILS=[Y]
022110 at 15:39:11:EXTERNAL_IF eth0 has private IP *********
022110 at 15:39:11:INTERNAL_IF eth1 has private IP *********
022110 at 15:39:11:dstar_g2_gwy v2.45 initialized...entering processing loop
[color=#BF0000]022110[/color] at [color=#BF0000]15:39:4[/color]8:START from rptr: cntr=53 ac, streamID=80,58, flags=00:00:00, my=[color=#BF0000]callsign[/color]   , sfx=[color=#BF0000]name[/color] , ur=CQCQCQ  , rpt1=GB3IN  C, rpt2=GB3IN  G, 58 bytes fromIP=172.16.0.2
022110 at 15:39:49:END from rptr: cntr=53 f8, streamID=80,58, 32 bytes
022110 at 15:39:53:START from rptr: cntr=53 fa, streamID=84,45, flags=00:00:00, my=G4TSN   , sfx=JON , ur=CQCQCQ  , rpt1=GB3IN  C, rpt2=GB3IN  G, 58 bytes fromIP=172.16.0.2
022110 at 15:39:55:END from rptr: cntr=54 68, streamID=84,45, 32 bytes
022110 at 15:40:02:START from rptr: cntr=54 6a, streamID=25,84, flags=00:00:00, my=G1ZGZ   , sfx=RICK, ur=CQCQCQ  , rpt1=GB3IN  C, rpt2=GB3IN  G, 58 bytes fromIP=172.16.0.2
022110 at 15:40:05:END from rptr: cntr=55 12, streamID=25,84, 32 bytes
022110 at 15:41:20:START from rptr: cntr=55 16, streamID=107,204, flags=00:00:00, my=G4TSN   , sfx=JON , ur=       U, rpt1=GB3IN  C, rpt2=GB3IN  G, 58 bytes fromIP=172.16.0.2
022110 at 15:41:21:END from rptr: cntr=55 4c, streamID=107,204, 32 bytes
022110 at 15:41:21:START from g2: cntr=00 00, streamID=20,134, flags=00:00:00, my=GB3IN   , sfx=RPTR, ur=CQCQCQ  , rpt1=GB3IN  C, rpt2=GB3IN  G, 56 bytes fromIP=82.153.52.51
022110 at 15:41:23:END from g2: cntr=00 65, streamID=20,134, 27 bytes from IP=82.153.52.51
022110 at 15:43:52:START from rptr: cntr=55 50, streamID=15,202, flags=00:00:00, my=G4TSN   , sfx=JON , ur=       U, rpt1=GB3IN  C, rpt2=GB3IN  G, 58 bytes fromIP=172.16.0.2
022110 at 15:43:52:END from rptr: cntr=55 63, streamID=15,202, 32 bytes
 
And need to parse the file to extract only the info in red date, time, callsign(my) and name(sfx)), into an array I can work with, then alter the format of the date and time to make it sql compatible and place the data into a database table.
Every other line in the log contains the data I need.


I am ok with the database side of things and I could work with the data from an array, manipulate it and place in the DB, but it is the parsing of the file into an array that I am lost with.

TIA
Dave
User avatar
F00Baron
Forum Newbie
Posts: 6
Joined: Sat Feb 20, 2010 11:43 am

Re: Help with parsing a file please.

Post by F00Baron »

I'm sure there is a very clever regex way but those are very confusing :)
Here's what I would do:

Code: Select all

 
$fh = fopen('logfile','r');
while(!feof($fh)) {
    $line = trim(fgets($fh,10240));
    if($line) {
        $parts = explode(' ',$line);
        $first_number = $parts[0];
        $timestamp = $parts[2];
        $my='';
        $sfx='';
        for($i=0; $i<count($parts); $i++) {
            if(strtolower(substr($parts[$i],0,2)) == 'my') {
                $my = substr($parts[$i],3);
            } else if(strtolower(substr($parts[$i],0,3)) == 'sfx') {
                $sfx = substr($parts[$i],4);
            }
        }
    }
}
fclose($fh);
?> 
wilded1
Forum Newbie
Posts: 9
Joined: Sun Feb 21, 2010 11:49 am

Re: Help with parsing a file please.

Post by wilded1 »

Thanks FOOBARON.

That has helped greatly.

This is what I have come up with so far and it is printing out the info I wanted.

Code: Select all

$fh = fopen('test1.log','r');
 while(!feof($fh)) {
     $line = trim(fgets($fh,10240));
     if($line) {
         $parts = explode(' ',$line);
         $date = $parts[0];
         $time = $parts[2];
         $call='';
         $name='';
         for($i=0; $i<count($parts); $i++) {
             if(strtolower(substr($parts[$i],0,2)) == 'my') {
                 $call = substr($parts[$i],3);
             } else if(strtolower(substr($parts[$i],0,3)) == 'sfx') {
                 $name = substr($parts[$i],4);
             }
         }
     }
     $month = substr($date, 0, 2);
     $day = substr($date, 2, 2);
     $year = substr($date, 4, 2);
 echo $day."/".$month."/".$year." ".$time." &nbsp; ".$call." - ".$name."<br />";
 }
 fclose($fh);
So I will just replace the echo with a query and insert the data into a table then I can do with it what I want.

Thank you again
Dave
Post Reply