Skip to content

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Some handy regular expressions (and attendant PHP code) that I've come across in my travels.
// some useful regular expressions and sample usage
// convert Twitter usernames into profile links
// cred. Simon Granade (
$text = preg_replace('/(^|\s)@(\w+)/',
'\1@<a href="\2">\2</a>', $text;
// convert Twitter hashtags into search links
// cred. as above
$text = preg_replace('/(^|\s)#(\w+)/',
'\1#<a href="\2">\2</a>', $text;
// a slightly improved hashtag regex - won't highlight pure-numeric hashtags, e.g. #123
// this seems to be the "correct" behaviour according to how Twitter does it
// cred. (Stack Overflow question referring to this very Gist, ooh!)
$text = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/',
'\1#<a href="\2">\2</a>', $strTweet);
// convert URLs into links
// includes URLs without http:// prefix
// cred. Allen Shaw +
$text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',
"<a href=\"$1\">$1</a>", $text);
$text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/i',
"<a href=\"http://$1\">$1</a>", $text);
// convert URLs into links, truncating long links (helps prevent wrapping fail)
// includes URLs without http:// prefix
// uses /e modifier to include functions in replacement - very handy
// regex itself cred. Allen Shaw +
$text = preg_replace('/\b([a-zA-Z]+:\/\/[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/ie',
"\"<a href='\\1'>\".(strlen('\\1') > 32 ? substr('\\1', 0, 32).'&hellip;' : '\\1').\"</a>\"",
$text = preg_replace('/\b(?<!:\/\/)(www\.[\w_.\-]+\.[a-zA-Z]{2,6}[\/\w\-~.?=&%#+$*!]*)\b/ie',
"\"<a href='http://\\1'>\".(strlen('\\1') > 32 ? substr('\\1', 0, 32).'&hellip;' : '\\1').\"</a>\"",
// match a reasonable selection of ASCII penises
$text = preg_replace('/(8|B|\(\)\))(=+)(D)(~*)/', '', $text);
// and for the ladies...
$text = preg_replace('/(\()( *)(\.)( *)(\)\(|\) *\(|Y)( *)(\.)( *)(\))/', '', $text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.