O'Reilly logo

Core PHP Programming: Using PHP to Build Dynamic Web Sites by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Regular Expressions in PHP Scripts

The basic function for executing regular expressions is ereg. This function evaluates a string against a regular expression, returning TRUE if the pattern described by the regular expression appears in the string. In this minimal form, you can check that a string conforms to a certain form. For example, you can ensure that a U.S. postal zip code is in the proper form of five digits followed by a dash and four more digits. Listing 16.2 demonstrates this idea.

Table 16-3. Square Brackets in Regular Expressions
SampleDescription
a.cMatches aac, abc, acc, ... — Any three-character string beginning with an a and ending with a c.
^a.*Matches any string starting with an a.
[a-c]*x$Matches x, ax, bx, abax, abcx — ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required