Random text

How would you pick a random piece of text from a text file and store it in a variable?

Comments

  • : 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:
    [red]foo
    bar[/red]
    [code]<?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.
    ?>
    [/code]
    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 " ".
  • : : 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:
    : [red]foo
    : bar[/red]
    : [code]<?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.
    : ?>
    : [/code]
    : 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
  • [code]<?php
    $fopened = fopen([blue]file[/blue], 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.
    ?>
    [/code]
    : 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.
  • [b][red]This message was edited by GameFreak7744 at 2004-6-23 22:6:25[/red][/b][hr]
    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?
  • the parameters of fopen are (string filename,string mode)
    so it's
    [red]'file'[/red],not file
    [red]'r'[/red],not r
    you can also use array_rand function:[code]echo"The random word is:".$exploded[array_rand($exploded)];[/code]
  • [b][red]This message was edited by GameFreak7744 at 2004-6-25 1:56:42[/red][/b][hr]
    : the parameters of fopen are (string filename,string mode)
    : so it's
    : [red]'file'[/red],not file
    : [red]'r'[/red],not r
    : you can also use array_rand function:[code]echo"The random word is:".$exploded[array_rand($exploded)];[/code]
    :

    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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories