interpim Posted January 2, 2008 Share Posted January 2, 2008 Hey guys... I've never been good at regular expressions, (I have no clue LOL) What I am trying to do is parse a log file from a video game. So far I have gotten to the point of counting the amount of lines that have the text "You Hit" in it, and echoing that to the output. What I am trying to do now, is pull the actual number from that line (this number varies in length) and get an average from all of those numbers. Can anyone explain to me how to do this? or point me in the direction of a tutorial I can follow to get the info I need? Here is a sample of the text file I am parsing. [15:16:13] You hit Crush for 611 damage! [15:16:13] You critical hit for an additional 458 damage! [15:16:15] You begin casting a Greater Rune of Shadow spell! [15:16:15] You are already casting a spell! You prepare this spell as a followup! [15:16:16] You hit Crush for 760 (+379) damage! my code so far counts the first and last lines but I want to add the 611 and 760 and get the average. Thanks in advance for any help offered Quote Link to comment Share on other sites More sharing options...
interpim Posted January 2, 2008 Author Share Posted January 2, 2008 Here is the code I have so far... Still can't figure out how to count the numbers in each line... <?php $target_path="uploads/"; $target_path = $target_path . basename($_FILES['userfile']['name']); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$target_path)){ echo "The file " . basename($_FILES['userfile']['name'])." has been uploaded<br>"; }else{ echo "There was an error uploading the file, please try again!<br>"; } $filename = "uploads/" . basename($_FILES['userfile']['name']); $handle = fopen($filename ,'r'); $hits = 0; $crits = 0; if ($handle){ while(!feof($handle)){ $buffer = fgets($handle,4096); if (preg_match("/You hit.*/", $buffer,$matches)){ $hits = $hits+1; } if (preg_match("/You critical hit.*/", $buffer, $matches )){ $crits = $crits+1; } } fclose($handle); echo "You hit " . $hits . " times.<br>You critted ". $crits . " times."; } ?> Quote Link to comment Share on other sites More sharing options...
interpim Posted January 2, 2008 Author Share Posted January 2, 2008 OK... i guess I was stressing myself out over nothing, I guess PHP automagically converts the text to integers when you pull them out of the string. If anyone is interested in how I did it, I exploded each line and pulled the number from the created array and then just added them to a variable. To easy I always expect to write 100 lines of code to do simple stuff ROFL. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.