[Previous] [Contents] [Next]


Using Regular Expressions

As you know some options for finding information within strings such as the strpos and substr. Although these functions are useful and efficient, they do have some limitations. Suppose, for example, that we have a form that requires a user to enter a U.S. or Canadian telephone number, of the format (###)###-####. We would have to pick apart this string one character at a time to make sure that the correct type of character was in the correct location. If we wanted to be flexible and allow input in formats such as 123.456.7890 or (123)456 7890, verifying these formats using only strpos and substr would take a frustrating number of function calls to verify the correct type of character in the correct location.

We would be much happier programmers if there were some advanced pattern-matching engine available to us in PHP. Of course, we would want it to be compatible with a multi-byte character set to help us with UTF-8 and other localized strings.

Fortunately, such functionality does exist in the form of a feature called regular expressions.


[Previous] [Contents] [Next]