What\’s Up

Random Quotes

leave a comment »

Here is a small script that I've used in the online slam book to display random quotes which reads the contents of a file and displays a random quote from it.

<?

$quotesfile = "quotesrandom.txt"; //The file in which all the quotes are stored.

$filecontents = fopen($quotesfile, "r"); // Open the file to read

$quotes = fread($filecontents, filesize($quotesfile)); // Read the file and store the contents of the file in $quotes

fclose($filecontents); // Close the file

if($quotes != "") { // Proceed if the file is not empty

$splitquotes = explode("n", $quotes); // Split the individual quotes into an array, explode using the new line character

$n = count($splitquotes); // Number of quotes available from the file

$quote = rand(0, $n - 1); // Generate a random number

$quote_display = explode("::", $splitquotes[$quote]); // Select a random quote using the random number and split the corresponding quote and author.

}

?>

<? echo $quote_display[0]; ?><div align="right">-<i><? echo $quote_display[1]; ?></i></div>

Here is the general format followed in the text file –

quote1::author1

quote2::author2

quote3::author3

quote4::author4

The symbol used in the file to seperate the quotes and the corresponding author is :: and any other symbol can also be used instead. Each quote is to be present in a new line in the text file.

Advertisements

Written by lokesh

May 14, 2006 at 5:45 am

Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: