Jump to content


Photo

Trying To Write Php Script From Perl Script...


  • Please log in to reply
4 replies to this topic

#1 Eternally777

Eternally777
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 June 2006 - 04:58 PM

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:

6/25 19:33:14.375  [EVENT]

One of the first things he does is issue the statement:

    # 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;

Thanks to his comments, I think I understand what he's doing here. Is there a way to do the same thing in PHP?

#2 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 27 June 2006 - 07:21 PM

[a href=\"http://www.php.net/manual/en/function.preg-match-all.php\" target=\"_blank\"]preg_match_all[/a]()
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#3 Eternally777

Eternally777
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 27 June 2006 - 09:59 PM

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:

6/25 19:30:02.640  EVENT.

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:

[PLAYERS]'s [ABILITY] hits you for [NUMBER] [TYPE] damage.

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?

#4 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 28 June 2006 - 03:01 PM

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:
/^(\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.

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:
/(\w+)'s (\w+) hits you for (\d+) (\w+) damage/
Will produce four matches...

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?
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#5 Eternally777

Eternally777
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 28 June 2006 - 05:52 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users