Skip to content

Instantly share code, notes, and snippets.

@BigglesZX
Created June 20, 2010 10:43
Show Gist options
  • Save BigglesZX/445729 to your computer and use it in GitHub Desktop.
Save BigglesZX/445729 to your computer and use it in GitHub Desktop.
Some handy regular expressions (and attendant PHP code) that I've come across in my travels.
<?php
//
// some useful regular expressions and sample usage
//
// convert Twitter usernames into profile links
// cred. Simon Granade (http://bit.ly/FihuS)
$text = preg_replace('/(^|\s)@(\w+)/',
'\1@<a href="http://twitter.com/\2">\2</a>', $text;
// convert Twitter hashtags into search links
// cred. as above
$text = preg_replace('/(^|\s)#(\w+)/',
'\1#<a href="http://search.twitter.com/search?q=%23\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. http://bit.ly/fve6Rv (Stack Overflow question referring to this very Gist, ooh!)
$text = preg_replace('/(^|\s)#(\w*[a-zA-Z_]+\w*)/',
'\1#<a href="http://search.twitter.com/search?q=%23\2">\2</a>', $strTweet);
// convert URLs into links
// includes URLs without http:// prefix
// cred. Allen Shaw + webmancers.com
$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 + webmancers.com
$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);
$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>\"",
$text);
// 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