Skip to content

Instantly share code, notes, and snippets.

@cange
Created January 6, 2010 22:30
Show Gist options
  • Save cange/270750 to your computer and use it in GitHub Desktop.
Save cange/270750 to your computer and use it in GitHub Desktop.
/**
* Twitter message String
* Searches in a string for links, replies, and hash and generates a <a>-Tag for the results.
* @param {String} message
* @return {String}
*/
function generateLinks ($message) {
$pattern[0] = '/(http:\/\/[0-9a-z-_.\/]+)/i'; # find is urls
$pattern[1] = '/@([0-9a-z-_]+)/'; # find is replies
$pattern[2] = '/#([0-9a-z-_]+)/'; # find is hashes
$replace[0] = '<a href="$1" title="external">$1</a>';
$replace[1] = '<a href="http://www.twitter.com/$1">@$1</a>';
$replace[2] = '<a href="http://search.twitter.com/search?q=$1">#$1</a>';
return preg_replace($pattern, $replace, $message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment