Jump to content


Photo

Loop through txt file, return value


  • Please log in to reply
6 replies to this topic

#1 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 27 October 2006 - 02:24 AM

Hey

Ive got a text file.  It has many lines of data.
I need a script that will loop through and if the line matches this:
String1.level=Int1
(and example would be: HAMM3R.level=4)
If Int1 >= 3, i need it to echo something like String1 = Int1

So, if a line is HAMM3R.level=4 then it will echo "HAMM3R = 4"
If its HAMM3R.level=2 it will go to the next line
And if the line doesnt match the String.level=Int format that it will also go to the next line

I know im not giving you anything to start with but im new at this.  I hope someone can help me out with this.

Thanks in advance!
HAMM3R

#2 bqallover

bqallover
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • LocationLeeds, UK

Posted 27 October 2006 - 02:56 AM

You need to use regular expressions for this kind of thing.  The following is untested but *should* work...

<?php
   
   $lines = file('data.txt');   // or whatever it's called

   foreach( $lines as $line )
   {
      if( eregi( "([a-z0-9]+)\.level=([0-9]+)", $line, $regs ) )
      {
         if( $regs[2] >= 3 )
         {
            echo "$regs[1] = $regs[2]\n";
         }
      }
   }

?>


#3 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 27 October 2006 - 03:31 AM

Great!  That works just as I needed.

Thanks bqallover  ;D

#4 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 27 October 2006 - 08:54 PM

Hey one more question.  Some of the users are in clans so have clan tags in their names.  Right now it wont display anything other than letters and number (as the regex allows).  Could you please show me how to make it accept [] = - | () etc?

Thanks!

#5 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 27 October 2006 - 09:06 PM

Try this...
<?php
   
   $lines = file('data.txt');   // or whatever it's called

   foreach( $lines as $line )
   {
      if(eregi( "(.*)\.level=([0-9]+)", $line, $regs); )
      {
         if( $regs[2] >= 3 )
         {
            echo "$regs[1] = $regs[2]\n";
         }
      }
   }

?>

PHP Ninja

#6 HAMM3R

HAMM3R
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 27 October 2006 - 10:30 PM

Thanks Caesar

#7 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 27 October 2006 - 10:35 PM

You're welcome. My pleasure.
PHP Ninja




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users