Back to vBulletin 3 Articles

Learn Regular Expressions
by filburt1 24 Apr 2003

This may look like pure nonsense:
PHP Code:
$s preg_replace("/\[mycode\](.*)\[\/mycode\]/siU""<b>\\1</b>"$s); 
...but it converts anything in $s that's in the format "[mycode]something[/mycode]" 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.

vblts.ru supports vBulletin®, 2022-2024