Skip to content

Instantly share code, notes, and snippets.

@and1truong
Forked from crittermike/gist:6a24108a65407676497e
Last active August 29, 2015 14:15
Show Gist options
  • Save and1truong/54ea656e69908daddb59 to your computer and use it in GitHub Desktop.
Save and1truong/54ea656e69908daddb59 to your computer and use it in GitHub Desktop.
<?php
/**
* Regex taken from http://saturnboy.com/2010/02/parsing-twitter-with-regexp/
*/
function parse_tweet($text) {
// Parse links.
$text = preg_replace(
'@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@',
'<a href="$1">$1</a>',
$text);
// Parse @mentions
$text = preg_replace(
'/@(\w+)/',
'<a href="http://twitter.com/$1">@$1</a>',
$text);
// Parse #hashtags
$text = preg_replace(
'/\s+#(\w+)/',
' <a href="http://search.twitter.com/search?q=%23$1">#$1</a>',
$text);
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment