Created
February 1, 2010 13:55
-
-
Save wilhelm-murdoch/291702 to your computer and use it in GitHub Desktop.
Properly parse tweets using PHP.
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
function parse($tweet) | |
{ | |
$regex = array | |
( | |
'#([a-z]{3,9}://[a-z0-9-_./\\\?&\+]*)#i', | |
'#[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}#i', | |
'#@([a-z0-9-_]+)#i', | |
'#\#([a-z0-9-_]+)#i' | |
); | |
$replace = array | |
( | |
'<a href="$0" target="_blank">$0</a>', | |
'<a href="mailto:$0">$0</a>', | |
'<a href="http://twitter.com/$1" target="_blank">$0</a>', | |
'<a href="http://twitter.com/search?q=%23$1" target="_blank">$0</a>' | |
); | |
return preg_replace($regex, $replace, $tweet); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment