News for hackers. Stuff that matters.
Re:Rails Template: Create a Twitter Application in Seconds by jsdalton on Friday April 03, 2009 @10:17AM (#544920)
I did one in PHP a few weeks back, which was a quick port of a Javascript version I found via Google:

    function twitterize_text($raw_text) {
      $output = $raw_text;

      // parse urls
      $pattern = '/([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/i';
      $replacement = '<a href="$1">$1</a>';
      $output = preg_replace($pattern, $replacement, $output);

      // parse usernames
      $pattern = '/[@]+([A-Za-z0-9-_]+)/';
      $replacement = '@<a href="http://twitter.com/$1">$1</a>';
      $output = preg_replace($pattern, $replacement, $output);

      // parse hashtags
      $pattern = '/[#]+([A-Za-z0-9-_]+)/';
      $replacement = '#<a href="http://search.twitter.com/search?q=%23$1">$1</a>';
      $output = preg_replace($pattern, $replacement, $output);

      return $output;
    }
Re:Rails Template: Create a Twitter Application in Seconds by Tichy on Friday April 03, 2009 @10:38AM (#544944)
Thanks, I think I can borrow some of your RegExp's ;-)
Re:Rails Template: Create a Twitter Application in Seconds by jsdalton on Friday April 03, 2009 @11:16AM (#544991)
Yeah, that's all I did too :)
Re:Rails Template: Create a Twitter Application in Seconds by there on Friday April 03, 2009 @12:36PM (#545129)
that will break on urls with @'s in them like some flickr urls
Re:Rails Template: Create a Twitter Application in Seconds by jsdalton on Friday April 03, 2009 @01:10PM (#545182)
You're right.

Looking closer, it also ignores URLs with a fragment after a #. Looking closer still, adding these (@ and #) to the regex would open a whole new can of worms, since the parsing statements below would attempt to convert these to URLs as well. As it stand now, it would probably mess up URLs with those characters in them, since they would be parsed as usernames or hashtags.

Hence the dangers of grabbing stuff off the Internet, as I did! If I have time to fix this later I'll post an update.

Re:Rails Template: Create a Twitter Application in Seconds by Tichy on Friday April 03, 2009 @03:46PM (#545478)
I thought it might not be perfect, but making it perfect is probably next to impossible. Here is a link with some monstrous URL matching Regexps: http://www.regexguru.com/2008/11/detecting-urls-in-a-block-o...
Re:Rails Template: Create a Twitter Application in Seconds by gord on Friday April 03, 2009 @10:53PM (#545983)
oh yeah... regex to match all urls == ugly hack.

Why didnt they just define urls by a simple FSM / regex in an RFP ?

Hey, I complain about the internet..but I do appreciate RFCs... Sweet lord, have a look at the UML / MOF spec - its hilariously vague and amorphic.