Saturday, September 22, 2007

Free Download RoboForm - Automate Logins - Remember Passwords

PHP Random Text 1.1

  • By: Nate Baldwin
  • For: PHP

Version 1.1 update changed to allow more than one instance of random content per page (up to 250 instances allowed).

This is a tutorial and free script to let you import random text of your choosing from a text file using PHP. You can import from an unlimited number of separate text blocks and can use either plain text, or HTML code. It requires that PHP be installed and running on your server. Below is a block of example text that uses this script (from 10 different text blocks). Refresh the page to see it load new text into the box...

Coming up with fun and exciting random text is, well, fun and exciting.

To get started download the script and example text files.

Decompress the .sit or .zip file, and you should find a GetRandomText.php and random.txt files. First, let's set up the random.txt file.

random.txt

This is a simple text file that you'll enter all you random text into. Open the example file, and you should see about ten lines of text separated by a *divider* string. Basically, you'll just want to open the file (in any text or HTML editor) and enter all the text you'd like to have imported randomly, and put a few characters in front of each line so the script knows where to separate them. It doesn't matter what character (or characters) you use as long as they don't appear anywhere else in the text. I like to use a string of characters so it's less likely to have an accidental match. For this example, I used *divider* (the word divider between to asterisks).

For this text file, you can either enter plain text or HTML code. You can even use HTML image tags if you want to import random images with the same script. Save the text file, and now we're ready to move on to the PHP script.

GetRandomText.php

You shouldn't need to make any changes to the PHP code in this file.

This file holds all the PHP code. There's very little you need to do to the file, except to tell it where the random.txt file is (in case you've moved it or changed its name), and tell it what the separation character (or characters) are that you used to divide your text blocks.

Open the GetRandomText.php file in any text or web editor (like Notepad, GoLive, Dreamweaver, etc.) and find line 5 of the script. It should by default look something like...

$MPTextFile = "random.txt";

By default, you don't have to change anything here, but if you changed the name of the random.txt file, you'll want to enter the new name of the file here inside double quotation marks. Next, enter the separation character(s) on line 6...

$MPSepString = "*divider*";

Make sure the character(s) are still inside double quote marks, and don't use double quotes anywhere in your separation string. Line 7 has one more editable option...

$MPTextToHTML = false;

If your random.txt file has HTML code in it at all, you'll want to leave this set to false. If your random.txt file has just plain text, then you'll want to change it to true (no quotation marks) so it can convert special HTML characters (such as " quotes, & ampersands, etc.) to their HTML equivalents.

If this options is set to false, then the script assumes you'll be supplying your own HTML code and will go ahead and strip any HTML, HEAD and BODY tags for you automatically and just bring in the blocks of code by themselves.

Using the Script

Now we're ready to use the script in our page. For best results, upload both the GetRandomText.php and random.txt file to the same folder as the page that you're importing the random text into. Then, make sure that your main page that imports the text has a .php extension (or whatever other extension your server required to run embedded PHP scripts).

Next, insert the following PHP code into your page (in HTML source code view) anywhere that you want to import random content:

include_once("GetRandomText.php");
$MPTextFile = "random.txt";
$MPSepString = "*divider*";
$MPTextToHTML = false;
MPPrintRandomText($MPTextFile, $MPSepString, $MPTextToHTML);
?>

The "include_once()" line imports the PHP from the GetRandomText.php file. If this file is not in the same folder as your page that's doing the importing, this will need to show the true path from the import page to your GetRandomText.php page instead of just the file name.

The next three lines are variables that control the script options. If you aren't using the default settings and file names, you will need to change the values of these variables as follows:

$MPTextFile = "random.txt";

By default, you don't have to change anything here, but if you changed the name of the random.txt file, you'll want to enter the new name of the file here inside double quotation marks. Next, enter the separation character(s) on line 6...

$MPSepString = "*divider*";

Make sure the character(s) are still inside double quote marks, and don't use double quotes anywhere in your separation string.

$MPTextToHTML = false;

If your random.txt file has HTML code in it at all, you'll want to leave this set to false. If your random.txt file has just plain text, then you'll want to change it to true (no quotation marks) so it can convert special HTML characters (such as " quotes, & ampersands, etc.) to their HTML equivalents.

Since PHP is a server-side language, the script will more than likely not work on your local machine until you upload the files to your server and test from there in your web browser. If you run into any problems, you're welcome to post a question in our Support Forum.

Good luck!

source: www.mindpalette.com

Thanks for visiting my blog, subscribe to my RSS feed. Thanks for visiting!