By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,965 Members | 1,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,965 IT Pros & Developers. It's quick & easy.

Parsing a text file

P: 11
Alright. The text file in question is an online game server's log file (log.log being the file, the game being Halo). It has information in it that I would like to be extracted and put in a table in a php file. It needs to be polled regularly (maybe every 30 minutes). Here is a piece of the log file:

Expand|Select|Wrap|Line Numbers
  1. 2007-02-16 21:58:42    KILL    ""    player 3    KILLED    "MastaSpoofa"    player 5
  2. 2007-02-16 21:58:43    CHAT    GLOBAL     [HIV]Cheefa    what map
  3. 2007-02-16 21:58:44    CHAT    GLOBAL     MastaSpoofa    LOL
  4. 2007-02-16 21:58:46    CHAT    GLOBAL     efw    lol on the gun
  5. 2007-02-16 21:58:52    CHAT    GLOBAL     MastaSpoofa    blood gulch
  6. 2007-02-16 21:58:53    CHAT    GLOBAL     MastaSpoofa    go
  7. 2007-02-16 21:59:00    CHAT    GLOBAL     MastaSpoofa    ice fields
  8. 2007-02-16 21:59:00    CHAT    GLOBAL     MastaSpoofa    go
  9. 2007-02-16 21:59:04    SCORE    CTF    player 4    ""    team 0
  10. 2007-02-16 21:59:04    PCR_TEAM    Team 0    Score    3
  11. 2007-02-16 21:59:04    PCR_TEAM    Team 1    Score    0
  12. 2007-02-16 21:59:04    PCR_PLAYER    Place 1    Player 4 ""    Team 0    Kills 6    Assists 1    Deaths 5    Score 2
  13. 2007-02-16 21:59:04    PCR_PLAYER    Place 2    Player 2 "pikachu"    Team 0    Kills 5    Assists 2    Deaths 4    Score 1
  14. 2007-02-16 21:59:04    PCR_PLAYER    Place 3    Player 6 "MastaSpoofa"    Team 1    Kills 6    Assists 0    Deaths 4    Score 0
  15. 2007-02-16 21:59:04    PCR_PLAYER    Place 4    Player 5 "[HIV]Cheefa"    Team 1    Kills 4    Assists 2    Deaths 3    Score 0
  16. 2007-02-16 21:59:04    PCR_PLAYER    Place 5    Player 3 "Isaac"    Team 0    Kills 3    Assists 2    Deaths 5    Score 0
  17. 2007-02-16 21:59:04    PCR_PLAYER    Place 6    Player 1 "efw"    Team 1    Kills 2    Assists 3    Deaths 9    Score 0
The information I want to extract are in the lines with "PCR_TEAM" and "PCR_PLAYER," and I want all of the information in those lines which comes after the aforementioned strings to be displayed in tables in a php file (and maybe the date and time on the line, as well). They contain the scores for each team and each player at the end of every game. There are several instances of these in the log file, and I would like each instance to have its own table.

Here's an example of how I would like the information to be displayed in a table:

Expand|Select|Wrap|Line Numbers
  1. 2007-02-16 
  2. 21:59:04
  3.  
  4. Place    Name        Team    Kills    Assists    Deaths    Score
  5.  
  6. 1            0    6    1    5    2
  7. 2    pikachu        0    5    2    4    1
  8. 3    MastaSpoofa    1    6    0    4    0
  9. 4    [HIV]Cheefa    1    4    2    3    0
  10. 5    Isaac        0    3    2    5    0
  11. 6    efw    1    2    3    9    0
  12. ------------------------------------------------------------------
  13.     Team 0                        3
  14.     Team 1                        0
Would someone be kind enough to write a php file which will do that? I know next to nothing about php, so the code for this would need to be complete (as opposed to being just a certain part of the file) for me to be able to utilize it.
Feb 19 '07 #1
Share this Question
Share on Google+
21 Replies


P: 99
Is there a way a line of the log can have properly seperated columns? now there are columns with five/four spaces in between and other with only one.

This will be needed to extract the data from every line.
Feb 19 '07 #2

P: 11
Is there a way a line of the log can have properly seperated columns? now there are columns with five/four spaces in between and other with only one.

This will be needed to extract the data from every line.
The deliminators used are tabs. I *think* that's what you're wanting to know.
Feb 19 '07 #3

P: 99
The deliminators used are tabs. I *think* that's what you're wanting to know.
i'm at work now but i will try to provide you with some code you need this evening.
Feb 19 '07 #4

P: 11
i'm at work now but i will try to provide you with some code you need this evening.
Thanks, I appreciate it. :)
Feb 19 '07 #5

P: 99
ok here we go :)

[PHP]
// fetches log files in an array per line
$lines = file('http://example.com/halo.log');
// two arrays to catch the things you need
$teams = array();
$players = array();
// fill the arrays
foreach($lines as $line){
if(preg_match('PCR_TEAM',$line)){ $teams[] = $line; }
if(preg_match('PCR_PLAYER',$line)){ $players[] = $line; }
}
[/PHP]

now whe have to get all the data out of the arrays

[PHP]
// get date from one of the arrays
$temp = $teams[0];
$temparr = split(' ',$temp);
$date = $temparr[0];
$time = $temparr[0];
// get teamdata out the team array
$teamnames = array();
$teamscore = array();
foreach($teams as $team){
$teamarr = split(' ',$team);
// this is a guess if the output isn't right adjust the numbers
$teamnames[] = $teamarr[3].' '.$teamarr[4];
$teamscore[] = $teamarr[6];
}
// get playerdata out of playes array
$playerplace = array();
$playername = array();
$playerteam = array();
$playerkills = array();
$playerassists = array();
$playerdeaths = array();
$playerscore = array();
foreach($players as $player){
$playerarr = split(' ',$player);
// this is a guess if the output isn't right adjust the numbers
$playerplace[] = $playerarr[4];
$playername[] = $playerarr[7];
$playerteam[] = $playerarr[9];
$playerkills[] = $playerarr[11];
$playerassists[] = $playerarr[13];
$playerdeaths[] = $playerarr[15];
$playerscore[] = $playerarr[17];
}
[/PHP]

and the final output

[PHP]
<p><?php echo $date; ?><br><?php echo $time; ?></p>
<table>
<tr><th>place</th><th>name</th><th>and so on</th></tr>
<?php
for($i = 0;$i<count($playerplace);$i++;){
echo '<tr><td>'.$playerplace[$i].'</td><td>'.$playername[$i].'</td><td>and so on</td></tr>';
}
for($i=0;$i<count(teamnames);$i++){
// change colspan if needed
echo '<tr><td colspan="4">'.$teamnames[$i].'</td><td>'.$teamscore.'</td></tr>';
}
?>
</table>

[/PHP]
Feb 20 '07 #6

P: 11
Thanks. I'm getting this error, though:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 17

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 19


It keeps repeating that over and over.
Feb 20 '07 #7

P: 99
Thanks. I'm getting this error, though:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 17

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in <url> on line 19


It keeps repeating that over and over.
sorry about that

[PHP]
foreach($lines as $line){

if(preg_match('PCR_TEAM',$line)){ $teams[] = $line; }

if(preg_match('PCR_PLAYER',$line)){ $players[] = $line; }

}
[/PHP]

should be

[PHP]
foreach($lines as $line){

if(preg_match('/PCR_TEAM/',$line)){ $teams[] = $line; }

if(preg_match('/PCR_PLAYER/',$line)){ $players[] = $line; }

}
[/PHP]
Feb 20 '07 #8

P: 11
All it's outputting is this:

"place name and so on
Array"

:(
Feb 20 '07 #9

P: 99
All it's outputting is this:

"place name and so on
Array"

:(
what does it output when you do

[PHP]
// after the first code block
print_r($teams);
// after the seccond code block
print_r($teamnames);
[/PHP]
Feb 20 '07 #10

P: 11
"Array ( ) Array ( )


place name and so on
Array"
Feb 20 '07 #11

P: 99
have you got the right link to your file?
Feb 20 '07 #12

P: 11
have you got the right link to your file?
Yeah, it's right.
Feb 20 '07 #13

P: 99
Yeah, it's right.
what gives

[PHP]print_r($lines);[/PHP]

as output?
Feb 20 '07 #14

P: 11
what gives

[PHP]print_r($lines);[/PHP]

as output?
Ok, that worked. It output the entire log file.

Edit: the line breaks are messed up, though.
Feb 20 '07 #15

P: 99
Ok, that worked. It output the entire log file.

Edit: the line breaks are messed up, though.
Can i see the output so i can catch where it goes wrong.
Feb 20 '07 #16

P: 11
Can i see the output so i can catch where it goes wrong.
Copying and pasting it isn't working for some reason, so here's a pic of the top piece of it:

Feb 20 '07 #17

P: 99
try this

[PHP]
foreach($lines as $line){
// this should give you two lines of output
if(preg_match('/PCR_TEAM/',$line)){ $teams[] = $line; echo $line; }

if(preg_match('/PCR_PLAYER/',$line)){ $players[] = $line; }

}
[/PHP]
Feb 20 '07 #18

P: 11
No luck. It just goes back to:

"place name and so on
Array"
Feb 20 '07 #19

P: 99
No luck. It just goes back to:

"place name and so on
Array"
can i have the link to the file or to a log file just like that. i will check it later.
Feb 20 '07 #20

P: 11
can i have the link to the file or to a log file just like that. i will check it later.
php file: http://www.hivclan.org/tests/ (index.php)
log file: http://www.hivclan.org/tests/halo.log
Feb 20 '07 #21

P: 99
What is the file format of of the log file? I tested it myself and i got strange characters? I checked it in my editor and i got UTF-16

http://xwerocode.110mb.com/halo.php

This is a test where i output the first 12 characters of each line. As you can see it's not 2007-02-17

I converted the log file to UTF-8 and i got this result after a few code changes

http://xwerocode.110mb.com/halo2.php

the code is

[PHP]
<html>
<head>
<title>log test</title>
</head>
<body>
<?php

// fetches log files in an array per line
// i used a converted log file
$lines = file('http://www.hivclan.org/tests/halo.log');


// two arrays to catch the things you need

$teams = array();

// fill the arrays

foreach($lines as $line){
if(preg_match('/PCR_TEAM/',$line)){ $teams[] = $line; }

}

// get teamdata out the team array

$teamnames = array();

$teamscore = array();

foreach($teams as $team){
$teamarr = split(chr(9),$team);
if(trim($teamarr[3]) == 'Score'){

$teamnames[] = $teamarr[2];

$teamscore[] = $teamarr[4];
}
}


?>

<table>

<tr><th>team</th><th>score</th></tr>

<?php

for($i=0;$i<count($teamnames);$i++){

// change colspan if needed

echo '<tr><td>'.$teamnames[$i].'</td><td>'.$teamscore[$i].'</td></tr>';

}

?>

</table>
</body>
</html>
[/PHP]

i noticed there are more than one tables to get generated from the log. this makes it a bit harder. more of a weekend job instead of a between the soup and potatoes job :)
Feb 20 '07 #22

Post your reply

Sign in to post your reply or Sign up for a free account.