public
Last active

Some handy regular expressions (and attendant PHP code) that I've come across in my travels.

  • Download Gist
Useful.Regexes.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
<?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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.