Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

PHP, preg_match and regex


  • Please log in to reply

#1
Taomech

Taomech

    Member

  • Member
  • PipPip
  • 61 posts
Hi all,
Working on a small project.
I'm new to PHP and I am trying to write a log parser.

The logs currently look like this:
3:40 say: Colonel Mustard: console
3:43 say: Colonel Mustard: it says
3:47 say: Colonel Mustard: console: what?
4:02 say: Colonel Mustard: k
4:07 say: Colonel Mustard: 4:07
1:35 say: ^#[RO^3DF]^!Taomech^!: need to se if sd works

And my current code is:
<?PHP

$game_file = fopen("c:/game.log", "r");

while (!feof($game_file)) {

$line_of_text = fgets($game_file);
//gets time, works
preg_match("/([0-9]+)\<img src='http://www.geekstogo.com/forum/public/style_emoticons/<#EMO_DIR#>/sad.gif' class='bbc_emoticon' alt=':D' />[0-9]+)/", $line_of_text, $time_matches);        
//matchs name, but onyl works if name is "name name" not "name".
preg_match("/say\:\s([a-z]+)\s([a-z]+)\:/i", $line_of_text, $name_matches_space);
preg_match("/say\:\s([a-z]+)\:/i", $line_of_text, $name_matches_nospace);

echo "Time is : {$time_matches[0]} Name is : {$name_matches_space[1]} {$name_matches_space[2]} {$name_matches_nospace[1]}. . . . . . . Original is : {$line_of_text} . <BR>";

}

fclose($game_file);

?>

Currently the script works, but is there a way to make it smaller?
Also, the actual logs I will be parsing are like the last one in the example, with the ^! etc. Is there a way to perform the match, while still keeping the ^! etc?

Thanks,
Taomech

Edited by Taomech, 20 September 2010 - 04:07 AM.

  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP