Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PHP

Display Featured Links Randomly Using PHP

0.00/5 (No votes)
8 Oct 2013GPL31 min read 6.2K  
Use PHP to read a CSV file and automatically generate links for display

I had a request to add a “Link of the Day” feature to one of the pages on the Law-related Education pages of the OBA website using technology we already have. I’m sure there are widgets out there already that will do this for me, and it may even be built into whatever CMS we deploy next, but I wanted to learn a bit so I decided to add it to our current site. I don’t know much about PHP, but I learned to code in VB.NET and C++, so I can learn enough as I go to make things work.

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 forums 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.

PHP
<?php
/**
 * Reads in a CSV file and displays a random link.
 *
 * @version 2.0.0
 * @author Morgan Estes <morgan.estes@gmail.com>
 * @license GPLv3
 */

/**
 * Use regex to parse the strings provided
 *
 * @deprecated eregi_replace replaced by preg_replace in PHP5
 * @param string $text
 *
 * @return 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;
}
/**
 *  Use regex to parse a string to make it a hyperlink
 *
 *  @param  string $text the text to turn into a hyperlink
 *  @return  string a hyperlinked version of $text
 */
function make_clickable_links( $text ) {
    $text = preg_replace( '/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
        '<a href="\\1">\\1</a>', $text );
    $text = preg_replace( '/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
        '\\1<a href="http://\\2">\\2</a>', $text );
    $text = preg_replace( '/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i',
        '<a href="mailto:\\1">\\1</a>', $text );
    return $text;
}

/**
 * Get file contents, run through a randomizer, and display on the screen
 *
 * @param string  $csv     path to file
 * @param string  $display inline or above
 * @return string link with text
 */
function display_link( $csv, $display = 'inline' ) {
    $fp = fopen( $csv, "r" );

    while ( !feof( $fp ) ) {
        $contents[] = explode( ",", fgets( $fp, 512 ) );
    }
    fclose( $fp );

    do {
        $x = rand( 0, count( $contents ) - 1 );
    } while ( $contents[$x] == '0' );

    $link = $contents[$x][1];
    $text = $contents[$x][0];

    if ( $display == 'above' ) {
        // displays link title above clickable URL
        echo "<span>$link<br>" . make_clickable_links( $link );
    } else {
        // displays link title as clickable link
        echo "<a href='$link' rel='nofollow'>$text</a>";
    }
}

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!

View a working sample of this code.

Posted in CodeProject, Web Development Tagged: clickableLinks, Comma-separated values, CSV, Link of the Day, PHP, Programming, random links, Web Development

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)