Created
January 21, 2013 22:04
-
-
Save coreyweb/4589922 to your computer and use it in GitHub Desktop.
Convert Twitter @mentions, #hashtags and URLs within a string to anchor links.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> </span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment