Now lets put what we learned about the explode() function and arrays into practice parsing (dealing with) files. Lets make a file called "file.txt" and fill it with at least 5 lines of non-sense. Open up SciTE or Notepad and type the following:
This is line 1
This is line 2
This is line ...uh... (what is after 2?)
anyways, this is the next line
And this is the last line!Make an new make a new file called "filetoarray.php". After you save it type the following into it:
<?php
// Store the file name in a string.
$filename = "file.txt";
// Read file into array
$contents = file($filename);
echo "there are <b>". count($contents). "</b> lines in this file. Here is what he array looks like now:<br>";
print_r($contents);
echo "<hr><b>Below are the lines in the file after they were \"shuffled\":</b><br>";
shuffle($contents);
// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
echo $line. "<br>";
}
echo "<hr>Now the SHUFFLED array looks like this:<br>";
print_r($contents);
?>After you run the file you should see two parts to the script. First is the Number of lines that are in the file and a print-out of what array element each line is in. Second you should see the lines you typed in file.txt showing up in a random order every time you refresh the page, with the print-out of what array element they are below them. So lets go over them!
<?php
// Store the file name in a string.
$filename = "file.txt";
?><?php
// Read file into array
$contents = file($filename);
?><?php
Array ([0] => This is line 1);
//Remember arrays start at "0" and go up for each element.
?><?php
Array ([1] => This is line 2);
?>Next we will use the count() function to count how many elements are in the array:
<?php
echo "there are <b>". count($contents). "</b> lines in this file. Here is what he array looks like now:<br>";
?><?php
echo "<hr><b>Below are the lines in the file after they were \"shuffled\":</b><br>";
shuffle($contents);
?>Last, we use a foreach loop to go through each element in the array and print out the line:
<?php
// Loop through the array and print each line to the screen.
foreach ($contents as $line)
{
echo $line. "<br>";
}
echo "<hr>Now the SHUFFLED array looks like this:<br>";
print_r($contents);
?>##################################################################
Now lets make something useful out of this....I know! how about a random quote?
(Like the one you see at the bottom of this page!)
Change the code in "filetoarray.php" to this:
<?php
// Store the file name in a string.
$filename = "file.txt";
// Read file into array
$contents = file($filename);
// Shuffle the elements to make the array random
shuffle($contents);
// Print out the first element.
echo $contents[0];
?>This is pretty simple to follow so I am just going to review some of it. After we have read each line into an array called "$contents" we just shuffle the arrays elements to make them random and print out the first element. (Which would be one of the lines in the file.)
#######################################################
You could also use something like this to choose a random banner or affiliate! So that is your home work - I want you to try to figure out how you could make a random image appear. I'll give you a major hint, 8) change "file.txt" to contain a new HTML image tag () for each line!
Wow I can't believe how easy it is to create a randomation array, I know this will be usefull for manythings on my website. and cheers for the heads up for creating random images :]
regards,
Vinnie