Jump to content

search strings for multiple instances of words, and add to variable.


interpim

Recommended Posts

I am using the following code to pull information out of a user uploaded file.

I thought at first it was working, but turns out it isn't counting every instance the word combination shows up, and if I try to add another if statement to look for a different set of words and increment that variable it usually breaks the output all together.

 

What am I doing wrong, and if there are any suggestions on how I could do this better, any help for this noob would be appreciated.

 

Also, is there a way to pull numbers out of a string to add up into a variable?

 

here is the code...

 

<?php
$file=fopen($uploadedfile,"r") or exit("Unable to open file!");
$melee_atk_you=0;
$melee_atk_them=0;
$rps=0;
$gold=0;
$evade_you=0;
$evade_them=0;
$parry_you=0;
$parry_them=0;
$block_you=0;
$block_them=0;
$miss_you=0;
$miss_them=0;
$bps=0;

while (!feof($file)) 
  {
  echo fgets($file), "<br>";
  if(stristr(fgets($file),"You perform")){
       $melee_atk_you=$melee_atk_you+1;}
  }
echo "You Evaded " , $evade_you , " times.<br>";
echo "You attacked " , $melee_atk_you , " times.<br>";
fclose($file);
?>

 

And here is a snippet of the type of file that will be passed to this page.

[01:06:00] You evade Matado's attack!
[01:06:00] (Region) You have entered Northern Suspiro Pass.
[01:06:01] You are now preparing to perform a Garrote style as a backup for Hamstring!
[01:06:02] You perform your Hamstring perfectly. (+32)
[01:06:02] A barrier absorbs 93 damage of your attack!
[01:06:02] You attack Matado with your axe and hit for 106 (-33) damage!
[01:06:02] A barrier absorbs 7 damage of your attack!
[01:06:02] You hit Matado for 3 damage!
[01:06:03] You prepare to perform a Leaper!
[01:06:04] Matado attacks you with his sword!
[01:06:04] Matado casts a spell!
[01:06:04] You perform your Leaper perfectly. (+34)
[01:06:04] You critical hit for an additional 46 damage!
[01:06:04] Matado looks resigned to their doom.
[01:06:05] You must wait 1 more minutes and 43 seconds to discharge an item!
[01:06:05] You are now preparing to perform a Garrote style as a backup for Hamstring!
[01:06:05] (Region) You have left Northern Suspiro Pass.
[01:06:07] You perform your Hamstring perfectly. (+37)
[01:06:07] Matado is bleeding!
[01:06:07] Matado attacks you with his sword!
[01:06:07] You are poisoned!
[01:06:07] Matado attacks you with his axe!
[01:06:07] Your movement is slowed!
[01:06:08] 140 style damage was blocked by the defender's magic shielding.
[01:06:08] You attack Matado with your falchion and hit for 238 (-49) damage!
[01:06:08] You attack Matado with your axe and hit for 192 (-61) damage!
[01:06:09] Matado attacks you with his sword! 

Instead of this:

 

if(stristr(fgets($file),"You perform"))

 

 

Try using this:

 

if ( ! stristr(fgets($file),"You perform") === false )

 

Maybe it can help. This example is from the php manual. I just added an " ! " before it. You may change false to true instead of adding an " ! "

 

Hope that helps.

that isn't working either... it is giving me the exact same outcome as before... 

 

It is not printing some of the file, and it isn't counting each instance of occurence correctly.

 

I changed it to this to test...

 

<?php
$file=fopen($uploadedfile,"r") or exit("Unable to open file!");
$melee_atk_you=0;
$melee_atk_them=0;
$rps=0;
$gold=0;
$evade_you=0;
$evade_them=0;
$parry_you=0;
$parry_them=0;
$block_you=0;
$block_them=0;
$miss_you=0;
$miss_them=0;
$bps=0;

while (!feof($file)) 
  {
  echo fgets($file), "<br>";
  if(!stristr(fgets($file),"You attack")===false){
       $melee_atk_you=$melee_atk_you+1;}
if (!stristr(fgets($file),"realm points")===false){
$rps=$rps+1;}
if(!stristr(fgets($file),"You evade")===false){
$evade_you=$evade_you+1;}
  }


echo "You Evaded " , $evade_you , " times.<br>";
echo "You attacked " , $melee_atk_you , " times.<br>";
echo "you earned RP's " , $rps, " times";
fclose($file);
?>

OK, the one snippet of code works fine by itself... but when I start adding other if statements it starts messing up the count for some reason.

 

Also, anyone know how I could pull out the numerical data in the file?

so I can use it in building averages etc.?

OK...

 

I have figured out my issue with losing some data because I didn't realize that once you call fgets() it automattically moves the pointer to the next line in the file.

 

I just added a fseek($file,0); after a while statement to start over for the next if statement.

 

Now my only issue is being able to pull numerical data from each of the strings... anyone that can clue me in on this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.