Jump to content

[SOLVED] parsing a text file, finding numbers and performing math on those numbers.


interpim

Recommended Posts

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 :)

 

 

Link to comment
Share on other sites

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.";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.