| px | top | add code | search | signup | login | help |
<?
// string randquote(string $infile, string $delim):
//
// generates a random quote, separated by $delim.
// $delim defaults to the fortune-style double-% on its own line (\n%%\n).
function randquote($infile, $delim = "\n%%\n")
{
// error trapping!
if(!IsSet($infile))
{
die ("You didn't specify a parameter for \$infile!");
}
// attempt to open $infile
if(!file_exists($infile))
{
die ("$infile was not found! check your paths!");
}
else
{
$fp = fopen($infile, "r")
or die ("Error opening file!");
}
// okay, let's get going.
$contents = fread($fp, filesize($infile));
$quote_arr = explode($delim,$contents);
fclose($fp);
// feed the Randomness Monster (tm)
srand((double)microtime()*1000000);
// generate random quote index
$quote_index = (rand(1, sizeof($quote_arr)) - 1);
// get quote at $quote_index and return it
$herequote = $quote_arr[$quote_index];
return $herequote;
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.