<?php
/* randomLineFromString by Devlin Palmer */
/* Example Usage:
randomLineFromString();
(use $string)
randomLineFromString("quotes.txt");
(grab a random line from quotes.txt)
*/
function randomLineFromString
($file =
false) {
/*
@author Devlin Palmer <lol@devlinsblog.com>
@param string $file The file you want to be read and parsed. If not supplied, $string below will be used.
*/
if ($file ===
false) {
// $string is the string that will be used if you do not specify a $file.
$string =
"Time is never time at all
You can never ever leave without leaving a piece of youth
And our lives are forever changed
We will never be the same
The more you change the less you feel
Believe, believe in me, believe
Believe that life can change
That youre not stuck in vain
Were not the same, were different tonight
Tonight, so bright
Tonight
And you know youre never sure
But youre sure you could be right
If you held yourself up to the light
And the embers never fade in your city by the lake
The place where you were born";
$string =
explode("\n",
$string);
$error =
false;
}
else
{
if (file_exists($file)) {
$string =
file($file);
$error =
false;
}
else
{
$error =
"randomLineFromString error: file cannot be opened.";
}
}
if ( $error !==
false ) {
print $error;
}
else
{
print $string[array_rand($string)];
}
}
?>