What are regular expressions? They are essentially string patterns. For a better explanation, check this out [http://en.wikipedia.org/wiki/Regular_expression
Knowing how to effectively write regular expressions is an extremely useful. You will also feel very powerful. Below are some possible uses:
1) Check if the text "IceMelon" appears in a string.
2) Check that an username contains only letters, numbers, or underscores.
3) Filter out URLs.
Let's talk about Use One. You can do this easily with the following expression:
I'll break this down. The expression you are searching for is contained within forward slashes (/). Okay, simple enough. But, what if you wanted the search to be case-insensitive? Add a 'i' to the end:
What if you wanted to check if an expression began with 'icemelon?'
The carrot '^' denotes start of a string. What if you wanted to check if something ended with 'icemelon?'
The dollar sign '$' denotes the end of a string. What if you wanted to check if a string contained either the words of 'ice' or 'melon' (in any order)?
If you want to present a selection of strings, put them in parenthesis and add |'s between each string.
Now, Let's look at Use Two. Consider the following scenario: You run a weblog service and want to limit usernames to only numbers, letters, and underscores. How do you do that? Check out this expression.
Wtf? Don't worry, I'll break that down.
» The '\w' signifies a "word" character, where a "word" is either a letter, number, or underscore.
» The '+' means 1 or more of the preceding character.
Altogether, the expression is checking that from the start till the end of the string, each character is either a letter or number. The '+' is very important. Without it, you would be looking for a one character string—in other words, a letter or a number. The '^' and '$' are also significant, because without them, a string like '##abcd##' would be acceptable.
Here are some alternatives to the "\w" syntax:
— any decimal digit
— any character that is not a decimal digit
— any whitespace character
— any character that is not a whitespace character
— any "non-word" character
— any character (NOTE: if you want to specify the period, you need to backslash it: i.e. '\.')
Here are some alternatives to he '+' syntax:
— zero or one of the previous character
— zero or more of the previous character
So, now that you're an expert, how do you put this shit to use? Check out these convenient built-in PHP functions:
Then, give Use Three a try yourself. If you haven't convinced yourself yet, regular expressions are really useful. In fact, I all those code block formatting and php.net links on the tutorials were generated using regular expressions. For example..
$tutorial = preg_replace("/URL\[(.+)\]/", "[<a href=\"\$1\" target=_blank>\$1</a>]", $tutorial);