473,372 Members | 888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Parsing a text file

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
21 9729
xwero
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
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
xwero
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
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
xwero
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
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
xwero
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
All it's outputting is this:

"place name and so on
Array"

:(
Feb 20 '07 #9
xwero
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
"Array ( ) Array ( )


place name and so on
Array"
Feb 20 '07 #11
xwero
99
have you got the right link to your file?
Feb 20 '07 #12
have you got the right link to your file?
Yeah, it's right.
Feb 20 '07 #13
xwero
99
Yeah, it's right.
what gives

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

as output?
Feb 20 '07 #14
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
xwero
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
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
xwero
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
No luck. It just goes back to:

"place name and so on
Array"
Feb 20 '07 #19
xwero
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
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
xwero
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

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

Similar topics

2
by: Joey Martin | last post by:
I have been reading documentation on parsing. I need some help though. I have the following in a text file: $650 Number of Bedrooms 3 Air Conditioning? Yes Original Ad SOUTH, 3BR, air, basement....
5
by: SROSeaner | last post by:
I have a text file that is the result of using XMLHTTP object to pull back a page of search results from a search engine. So I have the entire results page in HTML, and want to break out each hit...
11
by: .Net Sports | last post by:
In VB.net, I'm trying to do a couple of things in a couple of different blocks of code. I need to take the first 25 characters of a text file, then append at the end some ellipses and a MORE link...
6
by: kevin | last post by:
I need to parse an third party supplied delimited or fixed width text file into a datatable. The delimiter may vary. I am currently using a SteamReader to read each line and, for delimited...
4
by: thenewuser | last post by:
Hi all, I am working on windows 2000 and using php 5.0 and apache 2.0.59. I am facing a problem while parsing a text file.Actually I am using a pop server for parsing an email.I am downloading...
2
by: DCDeshpande | last post by:
Hi, I am new to Java and need to parse a simple txt file into unique tokens and store them in a data structure. Can someone please recommend the most efficient way to do this? I found out that...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
2
by: sevak316 | last post by:
Hello, I stored a text file into an array. Now I am going through the array looking for a particular string. When I find that string, I want certain things that come after it to be stored into a...
18
by: lka527 | last post by:
In the code, I have bold text where I have a question. I am not sure how to parse the file into different fields (right below with bold: machine,plan,study,scantype, date)... and I have more...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.