Snipe.Net - Geeky Stuff

PHP Regex to Make Twitter Links Clickable

closeThis post was published 2 years 4 months 15 days ago. There is a chance that some APIs or software versions discussed have changed since this article was written.

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

[sourcecode language='php']function twitterify($ret) {
$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);
return $ret;
}[/sourcecode]

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:

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.

Also check out:

If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!) My entire concept of self-worth is in your hands, so that makes you kind of a big deal. Srsly.

This entry was posted on Thursday, September 10th, 2009 at 4:44 am and is filed under PHP/mySQL, Web Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
  • http://twitter.com/cuneytgurcan Cuneyt Gurcan Akcora

    I want you to know that i LOVE you! Finally I found something that works!!!

  • http://www.snipe.net snipe

    Yes, of course. Smart quotes are not recognized as quote marks.

  • Deratrius

    Nice work, noticed that if there is a regular link between parentheses the the link isn’t parsed.
    eg: http://www.link.com is parsed but (www.link.com) isn’t.

  • Nic

    Thanks, this is what i’m looking for.

  • http://twitter.com/qu4cks0ft Airlangga bayu s.

    Thanks, function work on my code

  • Lucian

    Awesome! Thank you so much for writing this! It’s perfect!

  • Blocki

    Thanks man! This really helped me a lot, I’m gonna use it in my own twitter app on facebook!

  • http://enjoyallthings.com/ Angela Geise

    Thanks so much for this! I’ve been working on a script for collecting tweets and I was missing functionality to get Twitter handles and hash tags clickable to where they need to go. This helps a ton!

  • Benny

    This helped a lot! Thanks!

  • Biketrails.nl

    Great stuff, really helped me out recreating my timeline using the twitter API and php/mysql.

    Check it out: http://ww.biketrails.nl

  • http://twitter.com/DOADarkAngel Darkest of Angels

    Very nice, tyvm

  • http://psdtomagento.net psd to magento