Forum Moderators: coopster

Message Too Old, No Replies

want to parse a text file

using php and then mysql to store info

         

GenSwat

12:35 am on Mar 11, 2009 (gmt 0)

10+ Year Member



here is an example of the text file

[8:14:13 PM] 69th_Tango}{awk:Ju-88A-4(0) seat occupied by 69th_Tango}{awk at 76293.63 89179.81
[8:14:13 PM] 69th_Tango}{awk:Ju-88A-4 loaded weapons '2xSC1000' fuel 25%
[8:14:13 PM] 69th_Tango}{awk:Ju-88A-4 in flight at 76358.555 89180.15
[8:14:19 PM] 69th_Marauder:Ju-88A-4(0) seat occupied by 69th_Marauder at 76287.63 89165.81
[8:14:19 PM] 69th_Marauder:Ju-88A-4 loaded weapons '2xSC1000' fuel 25%
[8:14:20 PM] 69th_Marauder:Ju-88A-4 in flight at 76578.055 89173.016
[8:14:21 PM] Merc2Wolf:Il-2_1941_Early(0) seat occupied by Merc2Wolf at 115797.34 99856.85
[8:14:21 PM] Merc2Wolf:Il-2_1941_Early loaded weapons '8xM13' fuel 50%
[8:14:22 PM] Merc2Wolf:Il-2_1941_Early in flight at 115851.125 99856.484
[8:14:26 PM] Merc1Snake:Il-2_1941_Early(0) seat occupied by Merc1Snake at 115784.34 99856.85
[8:14:26 PM] Merc1Snake:Il-2_1941_Early loaded weapons '216xAJ-2' fuel 25%
[8:14:26 PM] Merc1Snake:Il-2_1941_Early in flight at 115805.33 99856.836
[8:14:27 PM] 69th_BGravey:Bf-109F-4 in flight at 77360.63 93018.24
[8:14:31 PM] 69th_Glow}{awk:Bf-109F-4 in flight at 77386.74 93105.84
[8:14:33 PM] 69th_B1izard:Bf-109F-4 in flight at 77281.52 93030.68
[8:14:35 PM] MercBish:La-5 in flight at 126114.805 92732.91
[8:14:37 PM] Merc4Wizz:La-5 in flight at 125787.97 92720.805
[8:14:37 PM] 69th_Hunter:Fw-190A-4 in flight at 77291.31 92703.734
[8:14:39 PM] 69th_needacar:Fw-190A-4 in flight at 77413.49 92494.555
[8:14:40 PM] Merc2aviator:La-5 in flight at 126068.91 92732.11
[8:14:42 PM] Merc1Reaper:La-5 in flight at 125634.3 92691.81
[8:15:01 PM] 69th_Slicer:Fw-190A-4 in flight at 79430.695 92916.46
[8:17:30 PM] 191_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99348.29 91923.62
[8:17:30 PM] 173_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99351.34 91946.76
[8:17:30 PM] 174_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99342.71 91946.89
[8:17:30 PM] 176_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99346.44 91918.99
[8:17:30 PM] 177_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99340.65 91923.36
[8:17:30 PM] 179_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99351.1 91919.98
[8:17:30 PM] 183_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99355.94 91922.18
[8:17:30 PM] 184_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99344.0 91948.78
[8:17:30 PM] 185_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99341.3 91947.06
[8:17:30 PM] 188_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99345.19 91949.91
[8:17:30 PM] 189_Static destroyed by 69th_Tango}{awk:Ju-88A-4 at 99352.24 91919.54

now i need to extract names planes there flew enemy killed by also there are point values

example 183_Static destroyed by 69th_Tango
183_Static is worth 40 points and 69th_Tangoe destroyed it. I need these items counted and totaled up I have another text file that has all the targets and there values

can someone give me a hint on where to start?

physics

12:48 am on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to write a regular expression (regex) that captures the info pieces.
See the preg_match function [us.php.net...]
and a regex reference like [regular-expressions.info...]
Then loop through the file, grabbing info with the regex, inserting it into the database as appropriate and totaling up the parts you need (or, you can wait until everything's in the database and use database queries to get your totals).

GenSwat

1:10 am on Mar 11, 2009 (gmt 0)

10+ Year Member



how do I grab there names, need an example to go off of, im fairly new to this and never used regex or preg-match

physics

1:37 am on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, take this line for example.

[8:14:21 PM] Merc2Wolf:Il-2_1941_Early(0) seat occupied by Merc2Wolf at 115797.34 99856.85

The trick is to break it down into it's essential parts.
[date] name:something something_else status_message decimal_number another_decimal_number
and write a regex that'll grab those parts.
So, a regex to just grab the date and the name and print them would be


preg_match('/\[(.*)?\]\s+(\w+):/',$line,$matches);

print "Date is: $matches[1], Name is: $matches[2]";


The regex part is the part between / and /.
Breaking it down

\[ # match a [ character (must be escaped with backslash)
(.*) # .* means match any character, as many times as possible
# enclosing it in quotes means SAVE THIS TO $matches
? # stop matching when you reach the next \]
\] # literally match a \]
\s+ # match one or more whitespace characters
(\w+) # match one or more alpha-numeric or underscore characters
# I'm assuming that your usernames fit this criteria
# the () means to remember all of the characters matched
: # match a colon character

Regular expressions are basically a language unto themselves and they are perfect for parsing text files. You'll want to study the syntax to figure out how to get at the pieces you want here.
Cheat sheets are very useful here:
PHP PCRE Cheat Sheet [phpguru.org]
There are also some tools that can help you craft regexes, for example "The Regulator" [sourceforge.net...]

p.s.
The p in preg refers to the fact that PHP uses Perl-compatible-regular-expressions (PCRE). So even though you're programming in PHP you may find many Perl references to be very useful ... keep that in mind when googling.

[edited by: eelixduppy at 2:44 am (utc) on Mar. 11, 2009]
[edit reason] fixed formatting [/edit]

physics

1:59 am on Mar 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Another good regex quick reference guide [PDF] [opencompany.org]

GenSwat

2:25 am on Mar 11, 2009 (gmt 0)

10+ Year Member



thanks alot

GenSwat

2:37 am on Mar 11, 2009 (gmt 0)

10+ Year Member



ok one more question I think, ok the plane there fly is after there name :Il-2_1941_Early now for this I have to write a new expression for each item I want out, or can I add to what you posted?

GenSwat

3:36 am on Mar 11, 2009 (gmt 0)

10+ Year Member



$events = file('events.txt');
$match = array();
$match['time'] = "(?P<time>\[.*\]\s)";

foreach ($events as $event)
{
if(preg_match("/{$match['time']}/", $event, $matches))
{
echo($matches['time'] . '<br />');
}
}

wel I got the time out of it now I tried to add the part you showed me and can't figure it out