Create Random Text in PHP
by Scott AllenWant to put a random quote on your site? Or randomize some other aspect of your site? It’s easy. You’ll want to use PHP or some other server-side scripting language instead of a client-side scripting language like JavaScript. Server-side languages write the content to the page in html before it ever leaves the server. Search engines will be able to read the content on your page, but with JavaScript they cannot, so you lose any content value of the random code. Also, if a user turns off JavaScript, that random content is lost. Google, MSN, and Yahoo will see that the content is changing often, and that may help your rankings (depending on your implementation).
Here’s how to put the random elements into your page, using PHP.
Step 1:
Create a text file called “myData.txt”. Open the file and put in your random elements, placing each random element on its own line, (whether text, images or html). Then place “myData.txt” in the same directory as your page containing the PHP script.
Step 2:
Place the following PHP code into your page where you want the random element:
<?php
// Read text file
$myData ="myData.txt";
// Replace "myData.txt" with your file
// Read Text file
$myText = file("$myData");
// Generate random text
$myTexts = rand(0, sizeof($myText)-1);
// Print out text
$myRandomText=$myText[$myTexts];
echo $myRandomText;
?>
You’re done! See an example of it in action below. (Reload the page to see another quote.)
Random Quote:
”I love it when a plan comes together.”
— Hannibal Smith, The A-Team
Tags:
php | seo | random quote | webgeek
If you enjoyed this post, make sure you subscribe to the RSS feed!
Related Posts:
About This Entry
You’re currently reading “Create Random Text in PHP,” an entry on WebGeek
- Published:
- 12.05.06 / 1pm
- Category:
- PHP, Web Design / Development
- Related Posts:
- Lorem Ipsum
- About
- End Blog Spam - Let Others Know About WP-SpamFree!
- Search Engine Optimization (SEO) Tools #2 - Robots.txt Generator
- SES NY Day 4 - Wrapping It Up, Along With Some Random Thoughts
- RSS Feeds:
- Subscribe to Blog
- Subscribe to Comments
- WordPress Plugins:
- WP-SpamFree
- About Us:
- Hybrid6 Studios is a
web design and SEO firm
based in Los Angeles, CA.- Hybrid6 Studios is a















No comments
Jump to comment form | comments rss [?] | trackback uri [?]