0
INFO_MAP name:"mapname" version:"1.2.3"
INFO_SETTINGS mode:"Mode_Normal" options:"Option_None"
PLAYER_CONNECT player:0 name:"testname" id:1231231 psr:1000.00
PLAYER_TEAM_CHANGE player:0 team:2
ITEM_PURCHASE time:0 x:13906 y:13485 z:110 player:2 team:2 item:"Item_ItemName" cost:250
ABILITY_UPGRADE time:0 x:13906 y:13485 z:110 player:2 team:2 name:"Ability_Example1" level:1 slot:2
ITEM_PURCHASE time:0 x:13869 y:13740 z:110 player:1 team:2 item:"Item_AnotherItem" cost:100
PLAYER_CHAT player:3 target:"team" msg:"Test Message with Spaces"

I have this style gamelogs which I have to parse via PHP. Which is the most efficient way to get something useful out of these, current strstr+explodes system is really terrible, coded by prior developer(s). I am thinking of regexp but unable to get anything to work.

2 Answers 2

1

A regex like this should work:

^([^\s]+)(\s+([^:]+):("[^"]+"|[^\s]+))+

Changed a bit for PHP (works for me with PHP version 5.1.6):

<?php
$pattern = '/([^\s]+)?(\s+([^:]+):("[^"]+"|[^\s]+))/';
$file_handle = fopen("logfile", "r");
while (!feof($file_handle)) {
   $line = fgets($file_handle);
   preg_match_all($pattern, $line, $matches);
   print_r($matches);
}
fclose($file_handle);
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It only grabs the mode + last arg/val combo.
I suppose in PHP you will have to use a preg_match_all and use this regex: ([^\s]+)?(\s+([^:]+):("[^"]+"|[^\s]+))
0

I would work trough the log line by line, getting the first action using substr and then process the rest using a function using explode or a preg_match_all. If this is exactly what the other programmers did, I think they did a good job.

Perhaps I wouldn't use regular expression at all, as it's inefficient when you have really large logfiles.

2 Comments

if(strstr($log_line, 'PLAYER_CONNECT')) { $line = explode(" ", $log_line); $id = explode(":", $line[1]); $name = explode(":", $line[2]);
Problem with explode(" ", $line) is that PLAYER_CHAT player:3 target:"team" msg:"Test Message with Spaces" <-- msg: part explodes too many times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.