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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing file for specific info.

    Ok first, I copied the console text from a game I've been playing. Now I want to make a PHP script that will search through the file line by line and check for 'Death Messages' where I've killed someone or where I've been killed. I want to be able to slice it all up and save who was killer, who was victim, and what the weapon was. Just having trouble coming up with the best way to go about it.. Any help or ideas would be greatly appreciated.. Heres the type of thing the console.txt file will have in it:

    Code:
    ^theLauncher was killed by THEELITISTFETUS's MP40
    |NuB| Shazbot: is this pub?
    THEELITISTFETUS was killed by SOMEDUMBGUY's Thompson
    THANA was blasted by ^theLauncher's Panzerfaust
    Allies have built the Oasis Water Pump!
    ^theLauncher failed to spot D3V1NC1's Landmine
    Allies capture the Old City!
    Smeegle dove on his own grenade.
    fentanyl was exploded by Smeegle's grenade

  • #2
    New Coder
    Join Date
    Sep 2005
    Location
    Latina (Italy)
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I can suggest you a way to filter your data.
    You could start with this core-function:
    PHP Code:
    <?php
    // given an array of strings, it recognizes the defeats and returns this kind of array:
    //    { [victim] => who's killed , [murderer] => the killer! , [weapon]=> what killed! }*
        
    function getMurderInfo$stringsArray ) {
            
    $murderReports = array();
            foreach( 
    $stringsArray as $report ) {
                
    $eregMatches = array();
                
    eregi('(.+) was killed by (.+)\'s (.+)',$report,$eregMatches);
                if( 
    count($eregMatches) > ) {
                    
    $murderReports[] = array( 'victim' => $eregMatches[1] , 'murderer' => $eregMatches[2] , 'weapon' => $eregMatches[3] );
                }
            }
            return 
    $murderReports;
        }
    ?>
    Then, you could use it in combo with something like this:
    PHP Code:
    <?php
        $battleInfoArray 
    file(/* your 'console.txt' file's exact path */);    // take data into an array
        
    $murderInfoArray getMurderInfo$battleInfoArray );    // interpret the data and filter for killings
        
    print_r$murderInfoArray );    // just to see if everything works
    ?>
    Obviously, instead of printing those data, you can save them into an xml file, or a relational db, or another text-file... whatever!

    I hope I've written something useful for you!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Just a quick addon
    Your going to have a tough time with this, as it appears to not be as consistant as you would like it to be. There are several usable phrases, 'killed', 'blasted', 'exploded', however they all appear to share one unique attribute, they all begin with 'was' and are followed by 'by'.
    I would instead create a simple preg_match_all function:
    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    $file './file.txt';
    if (
    is_readable($file))
    {
         
    $kills_file file_get_contents($file);
    }

    // This is whats considered to be an 'allowed' killing method:
    $allowed_methods = array(
         
    'killed',
         
    'blasted',
         
    'exploded',
    );
    // You can add to this if nessessary.

    function get_kills($kill_file '')
    {
         global 
    $allowed_methods;
         
    preg_match_all("/(.*)was(.*)by(.*)\'s(.*)/i"$kill_file$matchesPREG_SET_ORDER);
         if (!empty(
    $allowed_methods) AND is_array($allowed_methods))
         {
              if (
    is_array($matches))
              {
                   for (
    $i 0$i count($matches); $i++)
                   {
                        if (!
    in_array(trim($matches[$i][2]), $allowed_methods))
                        {
                             unset(
    $matches[$i]);
                        }
                   }
              }
         }
         return 
    $matches;
    }

    $murder_report get_kills($kills_file);

    ?>
    This will create an associative array of all kills involved with respect to the $allowed_methods array. Note, that if allowed methods is containing empty string values for its values, these will not work proper. You can of course, just delete the enire $allowed_methods array with no consequence.

    BTW:
    $matches[$i][1] = The Victim
    $matches[$i][3] = The Killer
    $matches[$i][4] = The Method.
    Hope that helps!
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Both of you have been a great help, thank you!


  •  

    Posting Permissions

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