Regular Expressions
PHP is a simple programming language with a lot of horsepower. That's why I love working with it.
But to take advantage of some of its most useful features, you need to have at least some basic knowledge of regular expressions.
Regular Expressions have a syntax that is complicated, convoluted, and cantankerous. But once you master them, you become powerful indeed.
For instance, to parse a form field to confirm that it is a legitimate email address, this piece of pHP code will do the trick:
eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,3}$", $_POST['email']))
In a nutshell, the eregi function (case-insensitive) uses regular expressions defined here to look in a form field called email for:
- alphanumeric characters, followed by
- the "@" symbol, followed by
- more alphanumeric characters, followed by
- a period, followed by
- two or three letters.
Powerful stuff indeed!