Jump to content

Trying To Write Php Script From Perl Script...


Eternally777

Recommended Posts

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?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.