PDA

View Full Version : Learn Regular Expressions


filburt1
24 Apr 2003, 00:54
This may look like pure nonsense:

$s = preg_replace("/\[mycode\](.*)\[\/mycode\]/siU", "<b>\\1</b>", $s);

...but it converts anything in $s that's in the format "something" to "<b>something</b>". This is thanks to regular expressions, a series of usually arcane-looking characters in a hideous and unreadable string that are extremely powerful once you know how to use them. vB uses them everywhere, and after I got over the very sharp learning curve, I use them everywhere I can now. You'd be amazed at how much it can simplify your code.

A regexp (or regex) reference that I use is at http://www.english.uga.edu/humcomp/perl/regex2a.html (that's for Perl but the expressions themselves are almost exactly the same). Also see http://www.php.net/preg_replace , http://www.php.net/preg_match , and http://www.php.net/preg_match_all , as well as all the other preg_...() functions.

Dean C
24 Apr 2003, 10:18
Good tip filburt. I know how useful they are now ;)

- miSt

Kriek
03 Jul 2003, 13:31
My Regular Expressions are rusty, but I wholeheartedly concur on their potency seeing that I now use them with REGEXP in MySQL queries, while previously only utilizing them within Perl or PHP. I highly recommend this (http://www.phpbuilder.com/columns/dario19990616.php3) article.