...

View Full Version : Parsing file for specific info.



dustin8420
11-08-2005, 09:01 AM
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:


^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

cdc08x
11-08-2005, 10:46 AM
Well, I can suggest you a way to filter your data.
You could start with this core-function:


<?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) > 0 ) {
$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
$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! :thumbsup:

Fou-Lu
11-08-2005, 04:36 PM
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
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, $matches, PREG_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!

dustin8420
11-08-2005, 07:18 PM
Both of you have been a great help, thank you! :) :) :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum