Skip to content

Instantly share code, notes, and snippets.

@roelven
Created April 19, 2010 12:20
Show Gist options
  • Save roelven/370979 to your computer and use it in GitHub Desktop.
Save roelven/370979 to your computer and use it in GitHub Desktop.
Function to fix the linking in tweets.
<?php
// Function to fix the links in tweets (@'s and #'s)
// From http://www.snipe.net/2009/09/php-twitter-clickable-links/
function fixTweet($tweet) {
$tweet = html_entity_decode($tweet);
$tweet = preg_replace('#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#', '\\1<a href="\\2" target="_blank">\\2</a>', $tweet);
$tweet = preg_replace('/(@([_a-z0-9\-]+))/i', '<a href="http://twitter.com/$2" title="View $2 on Twitter" target="_blank">$1</a>', $tweet);
$tweet = preg_replace('/(#([_a-z0-9\-]+))/i', '<a href="http://search.twitter.com/search?q=%23$2" title="Search $1 on Twitter" target="_blank">$1</a>', $tweet);
return $tweet;
};
print fixTweet($tweetBody);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment