Skip to content

Instantly share code, notes, and snippets.

@Olical
Created July 26, 2011 15:21
Show Gist options
  • Save Olical/1106998 to your computer and use it in GitHub Desktop.
Save Olical/1106998 to your computer and use it in GitHub Desktop.
Converts a plain old tweet to a fancy one with links all over the place.
<?php
/**
* Adds links to tweets
*
* @param string $tweet The raw tweet
* @return string The tweet with links
*/
function linkify($tweet)
{
// Link to URLs
$tweet = preg_replace('/((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)/', '<a href="$1">$1</a>', $tweet);
// Link to users
$tweet = preg_replace('/@([a-z0-9_]+)(\s|$)/i', '<a href="http://twitter.com/$1">@$1</a>', $tweet);
// Link to hash tags
$tweet = preg_replace('/(^|\s)#([^\s]+)/', '$1<a href="http://twitter.com/search?q=%23$2">#$2</a>', $tweet);
// Return the tweet with links
return $tweet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment