
In a current project I’m reading in a user’s Twitter feed and displaying it on the hompage of their Wordpress blog. I know that there are a thousand plug-ins that exist to do this exact thing, but I wanted to write my own.
Using the official Twitter API the contents of the individual were returned as plain text, meaning that all HTML links were totally dead. I wanted a way to automatically add the HTML to enable URL’s as well as twitter “@” profile links. I found some regular expressions and with some tweaking came up with these 2 functions.
To activate the URLs in block of text pass it to the following function:
function activateURLS($tweet) { return preg_replace("@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@", "<a href="$1" target="_blank">$1</a>", $tweet); }
To activate Twitter @ profile links using the following PHP block:
function activateTwitterProfiles($tweet) { return preg_replace("/@([^ ,.!#$%^&]+)/", "<a href="http://twitter.com/$1" target="_blank">@$1</a>", $tweet); }
They’re super easy to use, just call them like this:
activateTwitterProfiles(activateLinks($tweet->text))
Mike Bijon:
February 21st, 2009 @ 7:47 pmHey Dane, it’s weird that I found this via Google, when I could have just emailed or IMd you for same…
Your solution for linkifying URLs and Twitter profiles works, but leaves a few corner-cases where added characters could sneak in (such as someone putting punctuation like “:”, “-”, or even “@profile2″ immediately after the “@profile”). Disclaimer: The URL/WWW parts are older and well-tested. The Twitter part is my tweak of yours:
—-
function makeTwitterificatedURL($msg) {
// HTTP & FTP links
$msg = eregi_replace(’(((f|ht){1}(tp://|tps://){1})[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
‘\\1‘,
$msg);
// WWW links
$msg = eregi_replace(’([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
‘\\1\\2‘,
$msg);
// Twitter @[username] links
$msg = eregi_replace(’(@[a-zA-Z0-9_]+)’,
‘@\\1‘,
$msg);
return $msg;
}
—-
Notes for anyone seeing this:
* The use of eregi_replace() is a code-base preference, you can use whatever works for you.
* My HTTP & WWW functions are based on characters in “valid” URLs, but could be enhanced with URLEncoding.
* My @Twitter-profile function is based on characters currently allowed in Twitter profile names (currently that is “var Q = /[a-zA-Z0-9_]/;” from here: https://assets1.twitter.com/javascripts/signup.js [use this to make readable: http://jsbeautifier.org/). If Twitter changes their username rules, mine will break.
—-
*(self-promotion)* I’m using this to grab co-workers sweet feed, check GeekStats on Twitter
Mike Bijon:
February 21st, 2009 @ 8:54 pmApparently I needed that disclaimer, I tested but didn’t read carefully. The Twitter @profile regex needed a tweak:
function makeTwitterificatedURL($msg) {
// HTTP & FTP links
$msg = eregi_replace(’(((f|ht){1}(tp://|tps://){1})[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
‘\\1‘,
$msg);
// WWW links
$msg = eregi_replace(’([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
‘\\1\\2‘,
$msg);
// Twitter @[username] links
$msg = eregi_replace(’@([a-zA-Z0-9_]+)’,
‘@\\1‘,
$msg);
return $msg;
}