Eternally777 Posted June 27, 2006 Share Posted June 27, 2006 I'm trying to convert this Perl script I found into a PHP script to help teach myself PHP (I'll be the first to admit, it would probably help even more if I knew Perl). The function of the perl script is to parse a text file that's several hundred lines long and sort the information into tables. Every line closely resembles this format:[code]6/25 19:33:14.375 [EVENT][/code]One of the first things he does is issue the statement:[code] # check for valid format if (/(\d+)\/(\d+) (\d+):(\d+):(\d+).(\d+) (.*)/) { # extract the data we want my $month = $1; my $day = $2; my $hour = $3; my $min = $4; my $sec = $5; my $message = $7;[/code]Thanks to his comments, I think I understand what he's doing here. Is there a way to do the same thing in PHP? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 27, 2006 Share Posted June 27, 2006 [a href=\"http://www.php.net/manual/en/function.preg-match-all.php\" target=\"_blank\"]preg_match_all[/a]() Quote Link to comment Share on other sites More sharing options...
Eternally777 Posted June 27, 2006 Author Share Posted June 27, 2006 Ok, thanks, next question:In my previous post you saw that I'm trying to parse a log file made up of several hundreds of lines. Each line follows the exact format:[code]6/25 19:30:02.640 EVENT.[/code]where "EVENT" represents a string. I asked the first question to make sure that the script only parses lines starting with a date and time. Now that that works, I need to parse the EVENT string itself for specific information.There are only a handful of different formats the EVENT string can be in, and each format has its own constants.An example of one of the formats for the EVENT string is this:[code][PLAYERS]'s [ABILITY] hits you for [NUMBER] [TYPE] damage.[/code]Everything in brackets is variable. How would I go about finding all the lines in the log that are in that format, and putting the those variables into PHP variables? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted June 28, 2006 Share Posted June 28, 2006 Since the "EVENT" lines are in different formats, you'll probably want to use preg_match_all() to get all the lines starting with your date/time format and then go through the resulting array of matches to find the lines you want.For the entire line:[code]/^(\d+\/\d+ \d+:\d+:\d+\.\d+)\s+(.+?)$/Will produce two matches, the first being the date/time and the second the rest of the line.[/code]Note there are two flags (PREG_SET_ORDER and PREG_MATCH_ORDER) that determine how the results are put into the matches array.To parse your EVENT example:[code]/(\w+)'s (\w+) hits you for (\d+) (\w+) damage/Will produce four matches...[/code]One way to take care of your described file would be to preg_match_all() with the first regular expression above (probably with the PREG_SET_ORDER flag), then go through the resulting matches array element by element with a list of if-statments and preg_match()es based on your various EVENT formulas (like the second regex example).Okay? Quote Link to comment Share on other sites More sharing options...
Eternally777 Posted June 28, 2006 Author Share Posted June 28, 2006 You couldn't possibly know how big of a help that is. I've been spinning my wheels trying to figure this stuff out for three days, but now I'm finally making progress again! Thanks a lot! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.