Home PHP and MySQL Tutorials Regular Expressions

PHP Tutorials - Regular Expressions

Introduction to Data Validation with Regular Expressions

Regular expressions enable you to find and extract more complicated pieces of information in your strings and do this in a multiple-byte character set environment.

The tutorial covers the following:

  • The definition of regular expressions and the basics of their use
  • Potential problems that might arise while using regular expressions
  • Use of regular expressions to perform data validation in a web applications
  • Functions that use regular expressions to perform more interesting and advanced tasks

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.


Table of content (tutorial index)

advertisement