Skip to content

Instantly share code, notes, and snippets.

@jflefebvre
Last active December 15, 2015 15:19
Show Gist options
  • Save jflefebvre/5281079 to your computer and use it in GitHub Desktop.
Save jflefebvre/5281079 to your computer and use it in GitHub Desktop.
Allows to detect mentions, hashtags and links in a tweet and create links.
<?php
function twitterify($str) {
$str = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $str);
$str = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $str);
$str = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $str);
$str = preg_replace("/#(\w+)/", "<a href=\"http://twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $str);
return $str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment