Skip to content

Instantly share code, notes, and snippets.

@coreyweb
Created January 21, 2013 22:04
Show Gist options
  • Save coreyweb/4589922 to your computer and use it in GitHub Desktop.
Save coreyweb/4589922 to your computer and use it in GitHub Desktop.
Convert Twitter @mentions, #hashtags and URLs within a string to anchor links.
<?php
// Convert URL's with protocol prefix
$tweet = ereg_replace("[a-zA-Z]+://([-]*[.]?[a-zA-Z0-9_/-?&%])*", "<a href=\"\\0\">\\0</a>", $tweet);
//Convert URL with just www.
$tweet = ereg_replace("(^| |\n)(www([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://\\2\">\\2</a>", $tweet);
//Convert # hashtags
$tweet = ereg_replace("(^| |\n)(\#([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1<a href=\"http://search.twitter.com/search?q=\\2\">\\2</a>", $tweet);
$tweet = str_replace("/#", "/", $tweet);
//Convert @ replies
$tweet = ereg_replace("(^| |\n)(\@([-]*[.]?[a-zA-Z0-9_/-?&%])*)", "\\1@<a href=\"http://www.twitter.com/\\2\">\\2</a>", $tweet);
$tweet = str_replace("/@", "/", $tweet);
$tweet = str_replace(">@", ">", $tweet);
// Make the time prettier
$createstamp = $post->get('pubDate');
$relative_time = niceTime(strtotime($createstamp));
?>
<?php echo $tweet ?> <span>posted <a href="<?php echo $tweet_link ?>"><?php echo $relative_time ?></a>&nbsp;</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment