Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help, search through a txt file, then actions upon finding strings.

    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:
    Code:
    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.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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...=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

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    k, ty, will look into it.

    i dont know perl at all....

  • #4
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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))

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    // 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?

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by fatrat; 06-21-2006 at 05:21 PM.

  • #8
    New Coder
    Join Date
    Jun 2006
    Location
    Sweden
    Posts
    49
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Try something like this...
    PHP Code:
    $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-
    )

    )

  • #9
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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) ?
    Last edited by fatrat; 06-23-2006 at 03:33 PM.

  • #10
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ......
    Last edited by fatrat; 06-23-2006 at 07:15 AM.

  • #11
    Regular Coder
    Join Date
    Oct 2005
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    $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
    }
    Last edited by fatrat; 06-23-2006 at 03:12 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •