On Sun, 2002-02-03 at 02:35, Mike Frazer wrote:
> A slightly less cumbersome method than the while() loop below would be a
> foreach() loop:
> 
> function check_file($filename) {
>     if (!$lines = file($filename)) {
>         return false;
>     }
>       foreach ($lines as $line) {
>         $num_pipes = substr_count($line, '|');
>         if ($num_pipes < 2 || $num_pipes > 4) {
>             return false;
>         }
>     }
>     return true;
> }
> 
> Same result, probably absolutely no measurable (and we're talking fractions
> of microseconds here) difference in speed, but much easier to read.

That's a matter of opinion. In general, I've found while() to be 
10-20% faster than foreach(), and the only reason foreach() makes any 
sense to me is because I've got a perl background. The style issue
could be argued for days--go with what works for you.


Torben

> Mike Frazer
> 
> 
> 
> "Lars Torben Wilson" <[EMAIL PROTECTED]> wrote in message
> 1012602758.3230.127.camel@ali">news:1012602758.3230.127.camel@ali...
> > On Fri, 2002-02-01 at 14:02, toni baker wrote:
> > > I would like to prevent users from uploading a file
> > > that contains more than 4 pipes or less than 2 pipes.
> > > The code below prevents users from uploading a file
> > > containing more than 4 pipes, but not less than 2
> > > pipes.  Should I use awk, ereg, or sed?  Thanks
> >
> > Unless you *have* to spawn processes to do this for you
> > for some reason, you can keep things a lot simpler by
> > doing it in PHP, something like this:
> >
> > <?php
> > error_reporting(E_ALL);
> >
> > function check_file($filename) {
> >     if (!$upload_file = file($filename)) {
> >         return false;
> >     }
> >     while (list(, $line) = each($upload_file)) {
> >         $num_pipes = substr_count($line, '|');
> >         if ($num_pipes < 2 || $num_pipes > 4) {
> >             return false;
> >         }
> >     }
> >     return true;
> > }
> >
> > $userfile = 'testpipes.txt';
> > if (check_file($userfile)) {
> >     echo "File passed.\n";
> > } else {
> >     echo "File failed.\n";
> > }
> >
> > ?>
> >
> >
> > Hope this helps,
> >
> > Torben
> >
> > > system ("/bin/cat $userfile|/bin/sed -n
> > > 's/.*|.*|.*|.*|.*|/&/p'> pipes.txt;
> > > $fd =fopen("pipes.txt", 'r');
> > > $pipes5=fgets($fd,50);
> > > echo ($pipes5);
> > > fclose($fd);
> > >
> > > if ($pipes5) {
> > >   print "wrong number of pipes";
> > > }
> > >
> > > The uploaded file below should not pass but it does:
> > >
> > > aaaaa|bbbbb|ccccc|ddddd|eeeee
> > > aaaaa|bbbbb|ccccc|
> > > aaaaa|bbbbb|ccccc|ddddd
> > > aaaaa|bbbbb
> > > aaaaa|bbbbb|ccccc|ddddd|eeeee
> >
> > --
> >  Torben Wilson <[EMAIL PROTECTED]>
> >  http://www.thebuttlesschaps.com
> >  http://www.hybrid17.com
> >  http://www.inflatableeye.com
> >  +1.604.709.0506
> >
> 
> 
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
-- 
 Torben Wilson <[EMAIL PROTECTED]>
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to