Posted By

shapeshifta on 02/17/10


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

hERB
redstorm
dantreacy


Regex to make Twitter links clickable


 / Published in: PHP
 

URL: http://www.snipe.net/2009/09/php-twitter-clickable-links/

  1. function twitterify($ret) {
  2. $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
  3. $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
  4. $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
  5. $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
  6. return $ret;
  7. }

Report this snippet  

You need to login to post a comment.