Skip to content

Instantly share code, notes, and snippets.

@wilhelm-murdoch
Created February 1, 2010 13:55
Show Gist options
  • Save wilhelm-murdoch/291702 to your computer and use it in GitHub Desktop.
Save wilhelm-murdoch/291702 to your computer and use it in GitHub Desktop.
Properly parse tweets using PHP.
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