Re: [PHP] randomly random

2011-04-05 Thread Stuart Dallas
On Tuesday, 5 April 2011 at 16:14, Jim Lucas wrote:
On 4/5/2011 7:07 AM, Kirk Bailey wrote:
> > OK gang, to spew a single line from a file of fortune cookies, I want to 
> > read it
> > and echo one line. While I found a 4 line code which gets it done, I thought
> > there was a preexisting command to do exactly that. Any feedback on this?
> 
> No, but it can be done in one line:
> 
>  
> echo array_rand(@file(@$filename), 1);
> 
> 
> # if you wanted to do a couple checks, you could do the following
> 
> if ( is_file(@$filename) && filesize($filename) > 0 )
>  echo array_rand(file($filename), 1);
> 
> ?>

This method will eat memory unless you have a very small file.

Personally I would use filesize to get the length of the file, fopen it, fseek 
to a random position, then track back to a newline and use fgets to get the 
line. Then fclose, obviously.

Simples.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/




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



Re: [PHP] randomly random

2011-04-05 Thread Jim Lucas
On 4/5/2011 7:07 AM, Kirk Bailey wrote:
> OK gang, to spew a single line from a file of fortune cookies, I want to read 
> it
> and echo one line. While I found a 4 line code which gets it done, I thought
> there was a preexisting command to do exactly that. Any feedback on this?
> 

No, but it can be done in one line:

 0 )
echo array_rand(file($filename), 1);

?>

Jim Lucas

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



Re: [PHP] randomly random

2011-04-05 Thread Richard Quadling
On 5 April 2011 15:07, Kirk Bailey  wrote:
> OK gang, to spew a single line from a file of fortune cookies, I want to
> read it and echo one line. While I found a 4 line code which gets it done, I
> thought there was a preexisting command to do exactly that. Any feedback on
> this?

motd

maybe.


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

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



[PHP] randomly random

2011-04-05 Thread Kirk Bailey
OK gang, to spew a single line from a file of fortune cookies, I 
want to read it and echo one line. While I found a 4 line code which 
gets it done, I thought there was a preexisting command to do 
exactly that. Any feedback on this?


--
end

Very Truly yours,
 - Kirk Bailey,
   Largo Florida

   kniht
  +-+
  | BOX |
  +-+
   think


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