
June 25th, 2004, 01:43 AM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
need help text parsing.
Hi all, basically im having great difficulties trying to parse a text files and build my array from it all, anyhow im not sure were the problem is totally but i think it may be in the preg_match_all or maybe it just wacked. anyhow heres a link to the file im trying to parse, http://24.235.247.34/log.txt , heres the code so far...
PHP Code:
<?php define('DATA_FILE', 'results.dat'); $result = array(); if (! isset($_GET['reset'])) if (! $result = unserialize(@file_get_contents(DATA_FILE))) { echo 'Warning: Unable to open "'.DATA_FILE.'" for reading. Continuing anyway.<br />'; } if (isset($_GET['parse'])) { foreach (glob('*.txt') as $strFilename) { if (is_file($strFilename)) { $strLog = file_get_contents($strFilename); $strLog = str_replace( array("\n","\r\n"), "\r", $strLog ); preg_match_all( '/-{40,}\r(.*?)\rD/', $strLog, $arrMatches ); foreach( $arrMatches[1] as $strWeapon ) { $arrWeapon = explode("\r", $strWeapon); foreach( $arrWeapon as $strData ) { if ($strData) { $arrData = preg_split('/[\s]+/', trim($strData)); $result[$arrData[0]][$arrData[1]]['Gauntlet'] += $arrData[2]; $result[$arrData[0]][$arrData[1]]['MachineGun'] += $arrData[3]; $result[$arrData[0]][$arrData[1]]['Shotgun'] += $arrData[4]; $result[$arrData[0]][$arrData[1]]['G.Launcher'] += $arrData[5]; $result[$arrData[0]][$arrData[1]]['R.Launcher'] += $arrData[6]; $result[$arrData[0]][$arrData[1]]['LightningGun:'] += $arrData[7]; $result[$arrData[0]][$arrData[1]]['Railgun'] += $arrData[8]; $result[$arrData[0]][$arrData[1]]['Plasmagun'] += $arrData[9]; $result[$arrData[0]][$arrData[1]]['BFG'] += $arrData[10]; } } } unlink($strFilename); } } if(!$fp = fopen(DATA_FILE, 'w')) { echo 'Error opening "'.DATA_FILE.'" for writing. Aborting.<br>'; exit(1); } if(!fwrite($fp, serialize($result))) { echo 'Error writing contents to "'.DATA_FILE.'". Aborting.<br>'; exit(1); } fclose($fp); } echo '<br>After:<br><pre>'; var_dump($result); echo '</pre>'; ?>
also here what my array looks likes so far, i can't seem to match the numbers with the weapons...
PHP Code:
array(7) { ["Shotgun"]=> array(1) { [":"]=> array(9) { ["Gauntlet"]=> float(7.5) ["MachineGun"]=> int(5) ["Shotgun"]=> int(7) ["G.Launcher"]=> int(0) ["R.Launcher"]=> int(18) ["LightningGun:"]=> int(2) ["Railgun"]=> int(0) ["Plasmagun"]=> int(0) ["BFG"]=> int(0) } }
definatly doing something wrong here. any ideas?
|