This is just a quicky post, not one of my usual long, rambling diatribes. This week is madness, even by my own absurd standards, but I didn’t want to miss jotting this down in case it might be helpful to others.
I’ve had varying degrees of success trying to find a series of preg_replace statements that would correctly replace output generated by Twitter’s RSS feeds (which do not contain any linking HTML) to autolink hyperlinks, @replies and hashtags, so I finally sat down and sorted it out myself.
The code below should correctly autolink all of the autolinkables in your PHP script:
- links @username to the user’s Twitter profile page
- links regular links to wherever they should link to
- links hashtags to a Twitter search on that hashtag
$ret = preg_replace(“#(^|[\n ])([\w]+?://[\w]+[^ \”\n\r\t< ]*)#", "\\1\\2″, $ret);
$ret = preg_replace(“#(^|[\n ])((www|ftp)\.[^ \”\t\n\r< ]*)#", "\\1\\2″, $ret);
$ret = preg_replace(“/@(\w+)/”, “@\\1“, $ret);
$ret = preg_replace(“/#(\w+)/”, “#\\1“, $ret);
Someday I’ll try to find the time to write a regex primer/tutorial. Regex is another one of the things, like SVN, that seems scary and incomprehensible to many people, but eventually it clicks and makes sense. In the meantime, if you’re interested in learning more about using Regex in PHP, check out the following resources:
More Regexy Goodness:
- Using Regular Expressions in PHP via regular-expressions.info
- Introduction to PHP Regex via phpro.org
- Using Regular Expressions in PHP via webcheatsheets.com
- 15 PHP Regular Expressions for Web Developers via catswhocode.com
Make sure you leave yourself some time to actually try out some examples, and dissect the examples they give so that you really grok what’s happening. Once it clicks, it seems so simple, you won’t believe you ever let it beat you up and take your lunch money.
Image by xkcd, via their awesome web store. The comic rocks my geeky face off. I buy my clothes there. So should you.
PS – still really, really hate posting code in WordPress. Even in HTML mode, it keeps converting my fscking special characters, which then get double/triple/etc converted.