...

View Full Version : Help, search through a txt file, then actions upon finding strings.



fatrat
06-20-2006, 09:59 PM
Hi, i would like to make a match reporting system for clan gaming website. When we finish a match I have txt file like this on my system:


06/20/06 19:52:50 ScriptLog: InventoryAttachment::SetMesh() DT_Mesh Mesh( A-Weapons3P.aks74u_3P )-->( A-Weapons3P.aks74u_3P )
06/20/06 19:53:14 ScriptLog: Console Message: [US Army] [5thP]1LT.FatRat reporting in: Hospital Back Stairs
06/20/06 19:53:15 ScriptLog: Console Message: [US Army] [5thP]1LT.FatRat reporting in: Hospital Back Stairs
06/20/06 19:53:19 ScriptLog: Console Message: [US Army] [5thP]PV2.FireBlade reporting in: Outside Escort Approach
06/20/06 19:53:25 ScriptLog: InventoryAttachment::SetMesh() DT_Mesh Mesh( A-Weapons3P.m4a1_3P )-->( A-Weapons3P.m4a1_3P )
06/20/06 19:53:34 ScriptLog: Console Message: [5thP]SGT.quickfire was shot by [5thP]PVT.Rob.
06/20/06 19:53:34 ScriptLog: -----------WARNING-----------
06/20/06 19:53:34 ScriptLog: AGP_Character::PlayWaiting() Got here without being in Dying
06/20/06 19:53:34 ScriptLog: -----------WARNING-----------
06/20/06 19:53:42 ScriptLog: InventoryAttachment::SetMesh() DT_Mesh Mesh( A-Weapons3P.aks74u_3P )-->( A-Weapons3P.m4a1_3P )
06/20/06 19:53:42 ScriptLog: InventoryAttachment::SetMesh() DT_Mesh Mesh( A-Weapons3P.m4a1_3P )-->( A-Weapons3P.aks74u_3P )
06/20/06 19:53:45 Log: APawn::PostNetReceive() AGP_Character Saved 2 New 6
06/20/06 19:53:45 Log: APawn::PostNetReceive() AGP_Character Saved 6 New 2

What i would like to do is upload and then be able to search through the file, find certain strings and then perform actions for each string found.

For example I could search for 'was shot by' and then get the name before and after to make a statement and keep a count of how many ppl each person has shot.



Basically:
How do i search for $string, then take the name before $string save it as $namea and the name after $string as $nameb.
If somebody can tell me that I think i can figure out the rest and how to repeat it for other strings,

tyvm.

Fumigator
06-20-2006, 10:58 PM
You're talking about making a text file parser. It's no simple task and involves using regular expressions to break down the text. Google "regular expression" and you'll see what you're up against.

http://www.google.com/search?hl=en&q=%22regular+expression%22&btnG=Google+Search

Here's a parser someone wrote for the game Dark Age of Camelot, with a link to the code. I think it's written in Perl. You might be able to find something in that code that helps...

http://www.cse.msu.edu/cgi-user/penney/parser.cgi

fatrat
06-20-2006, 11:07 PM
k, ty, will look into it.

i dont know perl at all....

fatrat
06-21-2006, 07:19 AM
eregi ("([0-9a-z]{1,30}) (was killed by) (.{1,30})" , $text, $regs))

that code will find:

MadMan was killed by [5thP]1LT.FatRat's grenade

but i cant get it to find:

[5thP]RCT.MadMan was killed by [5thP]1LT.FatRat's grenade.

plz help!! :)

i tried, but it didnt work:

eregi ("([[0-9a-z\]]{1,30}) (was killed by) (.{1,30})" , $text, $regs))

fatrat
06-21-2006, 08:43 AM
nvm

got this to work:

(eregi ("([^:]{1,40}) (team leads the tournament) ([0-9]{1,2}) (to) ([0-9]{1,2})" , $value, $regs))

it takes all words between ':' and 'team leads the tournament', so i can get the name, yay to me. thanks for pointing me in the right direction.

k, now my only problem is the $value variable, for example:

$variable="Log 1:fsfsfsf
log 2: "hi"
log 3: 'bye'";

the variable contains " and ' , how do i strip these, coz it gives an error, ty.

fatrat
06-21-2006, 10:02 AM
// open url
$fh = fopen("http://www.the5thplatoon.com/armyops.txt", "r");

// if no errors, read contents
while (!feof($fh)) {
$output = fgets($fh);

//echo"$output";

$data = explode("\n", $output);
foreach ($data as $value){
//if (eregi ("( Console Message:) (.{1,100})" , $value, $regs)) {
//echo "$regs[1] $regs[2]<br>";
//}
if (eregi ("([^:]{1,20}) (was killed by) (.{1,30})" , $value, $regs)) {
echo "<br>$regs[1] $regs[2] $regs[3]<br>";
}
else if (eregi ("([^:]{1,20}) (was shot by) (.{1,30})" , $value, $regs)) {
echo "<br>$regs[1] $regs[2] $regs[3]<br>";
}
else if (eregi ("([^:]{1,20}) (left a small crater)" , $value, $regs)) {
echo "<br>$regs[1] $regs[2]<br>";
}
else if (eregi ("([^:]{1,20}) (killed himself.)" , $value, $regs)) {
echo "<br>$regs[1] $regs[2]<br>";
}
else if (eregi ("([^:]{1,20}) (was blown up by) (.{1,30})" , $value, $regs)) {
echo "<br>$regs[1] $regs[2] $regs[3]<br>";
}
else if (eregi ("([^:]{1,30}) (team leads the tournament) ([0-9]{1,2}) (to) ([0-9]{1,2})" , $value, $regs)) {
echo "<br>$regs[1] $regs[2] $regs[3] $regs[4] $regs[5]<br>";
}
}
}
// close
fclose($fh);

can i speed this code up?

fatrat
06-21-2006, 04:18 PM
k, im really stuck on getting the players names, plz help
this is a sample line:

06/20/06 22:34:04 ScriptLog: Console Message: AOD_AZT3K was shot by {BnB}$corpion.
06/20/06 22:34:04 ScriptLog: Console Message: [FoB]Champs was killed by fragments from -kef-'s grenade
what id like is:
$reg[1]=AOD_AZT3K
$reg2=was shot by
$reg3={BnB}$corpion

eregi ("([^:]{1,20}) (was shot by) (.{1,25})" , $value, $regs))

the problem this code i have used is that it includes the space after

Message :

and it includes the . at the end

plz how can i stop that, ty.

Nicklas
06-21-2006, 06:59 PM
Try something like this...

$log = file_get_contents('log.txt');

preg_match_all('/(?<=: )(.{1,25}) (was (?:shot|killed) by) (?:fragments from )?([^\s].{1,25})(?=\.|\'s)(?:.*?)(?=\s|\n)/s', $log, $match);
unset($match[0]);
print_r($match);

It generates an array that for example, looks something like this:

Array
(
[1] => Array
(
[0] => [5thP]SGT.quickfire
[1] => AOD_AZT3K
[2] => [FoB]Champs
)

[2] => Array
(
[0] => was shot by
[1] => was shot by
[2] => was killed by
)

[3] => Array
(
[0] => [5thP]PVT.Rob
[1] => {BnB}$corpion
[2] => -kef-
)

)

fatrat
06-22-2006, 05:07 PM
tyvm for that help it works well except for 2 things.
there is a bug with that line, for some reason this line:

06/23/06 11:11:54 ScriptLog: Console Message: liho777 was shot by NYX]^R[FidaeX]^CZ's AK47 Rifle [71]
makes:
Console Message: liho777 was shot by NYX]^R[FidaeX]^CZ
instead of:
liho777 was shot by NYX]^R[FidaeX]^CZ

edit:I changed the start of the preg to 'Console Message: ' instead of just ':' and that seems to of cured it.



and another bug:

.opC]^Headhunter's grenade
instead of:
.opC]^Headhunter

edit: fixed this bug, ([^\s].{1,25})(?=\.|\'s), changed the 25, to 20(thats the max name length anyway), i guess it was stopping at the '.' after 'grenade.' so now it doesnt get there i guess and stops at the " 's "





the line as i have it now:


preg_match_all('/(?<=Console Message: )(.{1,25}) (was (?:shot|killed|blown up|killed himself|left a crater|bled to death) by) (?:fragments from )?([^\s].{1,20})(?=\'s|\.)(?:.*?)(?=\s|\n)/s', $log, $match);

if: fatrat bled to death
will this be picked up, coz it doesnt start 'was' or finish 'by', how do i make them optional if so?
do u (?:was) and (?:by) ?

fatrat
06-22-2006, 09:05 PM
......

fatrat
06-23-2006, 01:10 PM
also would it be possible to extract these line:
06/23/06 11:11:54 ScriptLog: Console Message: team A leads the tournament 7 to 0.



so that you getsomething like:

Array
(
[1] => Array
(
[0] => team A
)

[2] => Array
(
[0] => leads the tournament

)

[3] => Array
(
[0] => 7 to 0
)

)



the page so far: http://the5thplatoon.com/members/matchinfo.php

1 of the problems i have is that i create a playerlist from the array[1] and array[3], then use that to get the total of kills and deaths for each player. The problem with this is that when '1st Squad leads the tournament 7 to 0', '1st Squad' and '7 to 0' will be counted as players. So the code needs to either put them in the array another way, like array[4][5][6], or i need to find a better way of counting the deaths and kills.

how i count deaths and kills:


$deadlist=array_unique($match[1]);
$killslist=array_unique($match[3]);
$playerlist=array_merge($deadlist,$killslist);
foreach($playerlist as $value){
$deathkeys=array_keys($match[1],$value);
$deaths=count($deathkeys);
$killkeys=array_keys($match[3],$value);
$kills=count($killkeys);
//here i write them to a html file, $value, $kills, $deaths
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum