Created
July 26, 2011 15:21
-
-
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.
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 | |
/** | |
* 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