PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Random text Posted by GameFreak7744 on 22 Jun 2004 at 1:47 AM
How would you pick a random piece of text from a text file and store it in a variable?

Report
Re: Random text Posted by Neak on 22 Jun 2004 at 8:11 AM
: How would you pick a random piece of text from a text file and store it in a variable?
:
:
This is how I would do it... roughly.
Let's say that the file looks like this:
foo
bar

<?php
$fopened = fopen(file, r); //Open the file... just for reading.
$string = fread(fopened); //Read it into a string.
$exploded = explode("/r/n", $string); //Separate it by newline into an array.
$rand = rand(0, count($exploded)); //Randomize a number between 0 and the number of elements of the array.
echo "The random word is:/n". $exploded[$rand]; // Echo element $rand of array $exploded
fclose(file); //Close it.
?>

That's how I would do it. It would either echo "foo" or "bar." Now, if you wanted to separate the words by spaces, not new lines, you would just change the "/r/n" to " ".
Report
Re: Random text Posted by GameFreak7744 on 22 Jun 2004 at 9:28 AM
: : How would you pick a random piece of text from a text file and store it in a variable?
: :
: :
: This is how I would do it... roughly.
: Let's say that the file looks like this:
: foo
: bar

:
<?php
: $fopened = fopen(file, r); //Open the file... just for reading.
: $string = fread(fopened); //Read it into a string.
: $exploded = explode("/r/n", $string); //Separate it by newline into an array.
: $rand = rand(0, count($exploded)); //Randomize a number between 0 and the number of elements of the array.
: echo "The random word is:/n". $exploded[$rand]; // Echo element $rand of array $exploded
: fclose(file); //Close it.
: ?>
: 

: That's how I would do it. It would either echo "foo" or "bar." Now, if you wanted to separate the words by spaces, not new lines, you would just change the "/r/n" to " ".
:


makes sense to me, only one problem - i dont know how to tell it *what* file to open :P
Report
Re: Random text Posted by Neak on 22 Jun 2004 at 12:20 PM
<?php
$fopened = fopen(file, r); //Open the file... just for reading.
$string = fread($fopened); //Read it into a string.
$exploded = explode("/r/n", $string); //Separate it by newline into an array.
$rand = rand(0, count($exploded)); //Randomize a number between 0 and the number of elements of the array.
echo "The random word is:/n". $exploded[$rand]; // Echo element $rand of array $exploded
fclose(file); //Close it.
?>

: makes sense to me, only one problem - i dont know how to tell it *what* file to open :P

Replace the word "file" in blue above with the path of the text file you want to look through.
Report
Re: Random text Posted by GameFreak7744 on 23 Jun 2004 at 9:58 PM
This message was edited by GameFreak7744 at 2004-6-23 22:6:25

i'm getting some big bad errors that i cant fix:

Warning: Wrong parameter count for fread() in /home/public2/public_html/indexframe.php on line 25
""
Warning: fclose(): supplied argument is not a valid stream resource in /home/public2/public_html/indexframe.php on line 29

any ideas?

EDIT: RE-the second error, um, should it be 'fclose($fopened)' rather than the file path?
Report
Re: Random text Posted by Johnny13 on 24 Jun 2004 at 10:52 PM
the parameters of fopen are (string filename,string mode)
so it's
'file',not file
'r',not r
you can also use array_rand function:
echo"The random word is:".$exploded[array_rand($exploded)];

Report
Re: Random text Posted by GameFreak7744 on 25 Jun 2004 at 1:50 AM
This message was edited by GameFreak7744 at 2004-6-25 1:56:42

: the parameters of fopen are (string filename,string mode)
: so it's
: 'file',not file
: 'r',not r
: you can also use array_rand function:
echo"The random word is:".$exploded[array_rand($exploded)];

:

yeah. i can also use 'file_get_contents' rather than 'fopen' and then 'fread'

EDIT:
I also find it better to use '#' insted of next line or space. however (no matter which kind i use) when it shows the last on in the file it adds a space to the end of it, any ideas how to remove this.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.