Random Quotes

Random QuotesLearn how to make Random Quotes using the power of PHP.

Many people email wondering how the heck we make the random quotes on the title bar at the main page. Well, we're finally releasing the secret, and its not as hard as you think :) Lets write the code to pick a random quote using PHP.

Here is what we have to write in English for the processing:

  1. Randomize everthing so the same quote doesn't come up.
  2. Use the preg split function to select a random text from a file. What we're doing here is loading the whole text file as one string, and then splitting a string into an array of quotes, everywhere --Next-- appears, it starts a new cell on the array.
  3. Select one of the cells in the array of quotes and print it out.

Here it is in PHP:

<?
srand((double)microtime()*1000000);
$arry_txt=preg_split("/--NEXT--/",join('',file("titles.txt")));
echo$arry_txt[rand(0,sizeof($arry_txt)-1)];
?>

Copy-paste that code at the top wherever you want the random quote to appear, in the title, in the body, anywhere :) The second part invloves making the titles.txt. That part is really easy to do. Open up Notepad and start making random quotes. Separate them by --Next--. Here are some to start you off:

Chalk Full of Vitamins Good for You
--NEXT--
Iron Chef Rules
--NEXT--
Spoonoholics Anonymous
--NEXT--
Spoono Feed Me
--NEXT--
The Envy of Silverware Everywhere
--NEXT--
Who Let the Spoono Out

Save these and upload them in the same directory as the file with the code above and you should be set to go. Not too tough was it? Well thats it folks. I hope it works out for you and if it doesn't, post on the board and we'll try to help you out.


Related Articles

  • Randomizer for ASP
    This article will show you how to create a simple but powerful randomizer.
  • Random Password Generator
    Random password generator.
  • Random Password Generation
    Web programmers will enjoy this article. It is a quick and effective way to create Random Password Generation - needed for security and anti-spam measures. The author provides concise lines of code, screenshots and simply read texts.
  • Banner Rotation Using PHP
    Creating a simple ad-rotator, the ads are stored in a text file and picked randomly by this PHP code. Most of the webmaster use banner ads, we will be creating a very simple banner rotator, which picks up randomly one ad from the banner file and displays it, this file can be called in any other page...
  • Random Images
    Ever wanted to make links, or banners appear on your site in a random order? Well heres a simple script that will do just that.
  • Banner Rotation Script
    Step1: Opening the Javascript tags and setting the variables/size of the images you are going to display:
    The width and heights are recognised as pixels, I have set the default size of most of the banners.
  • JavaScript Prompts
    Well, lets say you wanted to get somebodys name before they saw the page, and then write their name on your page right before their very eyes... Well, you can do this using a javascript prompt. Heres the command:
  • The Advantages of Dynamic Website Content
    Think about your own surfing behavior. What types of websites do you visit the most often; which ones keep drawing you back? If you are like most internet surfers, you will spend much of your time hanging around websites with dynamic website content, or content that is updated constantly...
  • Choosing A Web Designer : A Plan to Guide You Through The Minefield
    Choosing a web designer can seem like a daunting task. They come in all shapes and sizes
  • onMouseover? What's This?
    Well, its time to try out your first javascript. This one is nice because we dont have to deal with adding the script tag. This little script will write something of your choice to the browsers status bar when you move your mouse over a link. Lets look at the example:

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.