Category Archives: Coding

PHP Tip: Replace URLs With Links

I was trawling through the PHP function reference the other day, looking for some information on regular expressions, when I happened across this gem:

$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\" rel=\"nofollow\">\\0</a>", $text);

It may look like gibberish at first, but if you look at it for awhile, you might be able to guess what it does.

Suppose you’re writing a PHP script that displays data, let’s use blog comments as an example. Sometimes the data being displayed may have a URL pasted into it by the user that submitted it. Wouldn’t it be nicer for the end user if URLs like that were automatically turned into clickable links, so “http://google.com” would become “http://google.com?”

That’s exactly what this line of PHP code does. It takes the variable $text and parses through it, converting stray URLs into clickable links.

PHP strtotime() Function

It’s always a pain to deal with dates when coding. Most date-oriented functions expect UNIX timestamps, which is hardly a user-friendly format, and at a glance it’s near impossible to do something simple in theory, such as adding three days to a date. Generally…

jQuery Color Fade Menu

Have you ever seen one of those Flash navigational menus where the hover color fades in and out? You know, the ones where you point to an item and it doesn’t just change color abruptly, but fades into the other color? Well, it’s possible…

AJAX, Disambiguation

“AJAX,” which stands for “Asynchronous Javascript and XML” has sadly been being used as a buzzword lately, referring to Javascript/DHTML tricks rather than it’s “real” meaning. AJAX is the use of the XMLHttpRequest object in JavaScript to load new content from a server into…

Learning MySQL [Book Review]

Almost anyone who’s played around with PHP before has run into the subject of databases. MySQL databases are the most common method of storing massive amounts of data to later be sorted through and retrieved for display via script. WordPress stores all of it’s…

The BOSS Mashable Challenge

A couple months ago, Yahoo released a promising new search API called Yahoo Search BOSS. It allows you to query results from Yahoo’s servers and display them how you want, reordering them even. No hard query limis are in place either. It’s pretty awesome.…

Print_r: A Very Useful PHP Function

Have you ever had an array or object returned by a PHP function, but you didn’t know how it was structured? In order to proceed writing your code, you would need to known what’s in the array, and how to access it. This somes…

The PHP Anthology: 101 Essential Tips, Tricks & Hacks [Book Review]

About a year ago, I purchased a copy of Sitepoint‘s book The CSS Anthology, which I enjoyed. While browsing through the “Computers” shelf in the local library’s New Books room, I came across a copy of The PHP Anthology. This isn’t your garden variety…

Yahoo BOSS API

Yahoo recently released a new search API. Known as Yahoo BOSS, for “Build your Own Searcg Service,” the API allows you to query search results from their servers, format them however you want, mash the data up with other services, and even re-order results.…

Safer Contact Forms Without CAPTCHAs

This is by no means a new post (May 2006), but it’s still relevant. Nobody likes CAPTCHAs. It can be hard to decipher the letters and they’re annoying. They help keep the spam down though. Well, there’s a better solution than forcing people to…