Display Featured Links Randomly Using PHP

Posted on June 29, 2009. Filed under: Main, Tech Tips, Web Development, Work Links | Tags: , , , , , , |

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 Web site using what we currently have in place.  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 implement it on 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 little 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 is loaded 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.


  1: <?php
  2: function makeClickableLinks($text) {
  3:  
  4:   $text = eregi_replace('(((f|ht){1}(tp|tps)://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  5:     '\\1', $text);
  6:   $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  7:     '\\1\\2', $text);
  8:   $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
  9:     '\\1', $text);
 10:   
 11: return $text;
 12:  
 13: } // end function
 14: 
 15: 
 16: function displayLink(){
 17: 
 18: $fp = fopen("your-file.csv", "r");
 19: while (!feof ($fp)) {
 20: $contents[] = explode(",", fgets($fp, 512));
 21: }
 22: fclose ($fp);
 23: 
 24: do {
 25: 	$x = rand(0, count($contents)-1);
 26: 		} while ($contents[$x] == '0');
 27: 
 28: // displays link title above clickable URL
 29: echo $contents[$x][0] . "<br />\n" . makeClickableLinks($contents[$x][1]) . "<br />\n";
 30: 
 31: // displays link title as clickable link
 32: echo '<a href="' . $contents[$x][1] . '" rel="nofollow">' . $contents[$x][0] . '</a>';
 33: 
 34: } // end function
 35: 
 36: ?>
 37: <body>
 38: <?php displayLink(); ?>
 39: </body>

Click to view a working sample of this page here.

WordPress Tags: Display,Links,Link,Education,code,Google,Internet,text,Click,Development,adjustments,makeClickableLinks,eregi_replace,displayLink,clickable

Read Full Post | Make a Comment ( 1 so far )

Don’t Forget to Clean Your Code!

Posted on June 17, 2009. Filed under: Main, Microsoft, Tech Tips, Web Development | Tags: , , , , , |

I recently was asked to update a Web page with a rather long list of names, which were provided in a Word document.  I use a combination of Dreamweaver and Expression Web, because they both have good tools for stripping out problematic HTML from Word, but today they weren’t much help.

This particular document had a table (of sorts) built into it, which listed names and cities, but after a good ol’

copy and paste, I found that things didn’t quite line up.  Upon closer review, I found that the “table” was actually made up of tabs and spaces.  While this worked in Word, it made a horrible mess for the Web.  Just doing a copy and paste job created spaces (technically non-breaking spaces — “nbsp”) that seemed like empty space, but actually take up quite a lot.  In fact, these spaces made the page 322kb!  Most of the larger pages on the site were around 20-25kb, so I took another look at things.

In this case, I decided not to go with a table for the little bit of text that could fit in, instead going with the city name in parentheses after the person’s name.  After removing all the nbsp’s from the code, I was able to shrink the page to 32kb – a 90% reduction in page size!  That means the page will load in just over 1 second, instead of over 12 from before.

So, to recap:

  1. Copy and paste doesn’t work as expected with the web, so be prepared to clean up your code a bit.
  2. Check for spaces that don’t belong, particularly the “ ” – they take up more space than just hitting the spacebar does.
  3. Use a tool like Firebug for Firefox, or the built-in tools in several other browsers (IE8, Safari 4, Chrome) to test your page for load times and see how you can improve them.

Windows Live Tags: Code,Word,Dreamweaver,Expression,tools,HTML,spaces,text,reduction,size,Copy,tool,Firebug,Firefox,Safari,Chrome,nbsp

WordPress Tags: Code,Word,Dreamweaver,Expression,tools,HTML,spaces,text,reduction,size,Copy,tool,Firebug,Firefox,Safari,Chrome,nbsp

Read Full Post | Make a Comment ( None so far )

  • August 2017
    S M T W T F S
    « Jun    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Meta

  • Categories

  • delicious tags

  • Blog Stats

    • 10,514 visits

Liked it here?
Why not try sites on the blogroll...