With the help of “The Google”, I was able to piece together a bit of code that reads from a CSV file into an array, then randomly displays a link from within that array on each page load, so that a new link gets displayed on each visit.
The original code has appeared in several forms across the Internet already, so if it’s yours, please let me know so I can credit you. I’ve made some slight adjustments to fit my needs.
[sourcecode language=”php” wraplines=”false”]
<?php
/**
* @function makeClickableLinks
* @param string
* @returns string
*/
function makeClickableLinks($text) {
$text = eregi_replace(‘(((f|ht){1}(tp|tps)://)[-a-zA-Z0-9@:%_+.~#?&//=]+)’, ‘\1’, $text);
$text = eregi_replace(‘([:space:[{}])(www.[-a-zA-Z0-9@:%_+.~#?&//=]+)’, ‘\1\2’, $text);
$text = eregi_replace(‘([_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3})’, ‘\1’, $text);
return $text;
}
/**
* @function displayLink
* @param string // path to csv
*/
function displayLink($csv) {
$fp = fopen($csv, "r");
while (!feof($fp)) {
$contents[] = explode(",", fgets($fp, 512));
} //!feof($fp)
fclose($fp);
do {
$x = rand(0, count($contents) – 1);
}
while ($contents[$x] == 0);
// displays link title above clickable URL
echo $contents[$x][0] . "n" . makeClickableLinks($contents[$x][1]) . "n";
// displays link title as clickable link
echo '<a href="'.$contents[$x][1].'" rel="nofollow">'.$contents[$x][0].'</a>';
}
?>
[/sourcecode]
UPDATE
WordPress keeps messing up code formatting, so I made it into a Github Gist.
UPDATE 2
Just learned of a shortcode for formatting code blocks on the hosted version of WordPress. Hooray for sourcecode!