Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
Internet & World Wide Web How to Program, 3/e
Internet & World Wide Web How to Program, 3/e

© 2004
pages: 1420

Amazon logo

This tutorial presents two PHP programs that introduce PHP's powerful string processing and regular-expression processing capabilities. The techniques shown here are used in the subsequent tutorial:
[Note: This tutorial is an excerpt (Section 26.3) of Chapter 26, PHP, from our textbook Internet & World Wide Web How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., INTERNET & WORLD WIDE WEB HOW TO PROGRAM, 3/E, 2004, pp.910-915. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
26.3   String Processing and Regular Expressions (Continued)
In addition to literal characters, regular expressions can include special characters that specify patterns. For example, the caret (^) special character matches the beginning of a string. Line 24 searches the beginning of $search for the pattern Now.
The characters $, ^ and . are part of a special set of characters called metacharacters. A dollar sign ($) searches for the specified pattern at the end of the string (line 29). The pattern Now is not found at the end of $search, so the body of the if statement (lines 30-31) is not executed. Note that Now$ is not a variable; it is a pattern that uses $ to search for the characters Now at the end of a string. Another special character is the period (.), which matches any single character.
Lines 34-35 search (from left to right) for the first word ending with the letters ow. Bracket expressions are lists of characters enclosed in braces ([ ]) that match a single character from the list. Ranges can be specified by supplying the beginning and the end of the range separated by a dash (-). For instance, the bracket expression [a-z] matches any lowercase letter, and [A-Z] matches any uppercase letter. In this example, we combine the two to create an expression that matches any letter. The special bracket expressions [[:<:]] and [[:>:]] match the beginning and end of a word, respectively.
The expression inside the parentheses, [a-zA-Z]*ow, matches any word ending in ow. It uses the quantifier * to match the preceding pattern zero or more times. Thus, [a-zA-Z]*ow matches any number of characters followed by the literal characters ow. Some PHP quantifiers are listed in Fig. 26.9.
Fig. 26.9 Some PHP quantifiers. (Part 1 of 2.)
Exactly n times.
Between m and n times inclusive.
n or more times.
One or more times (same as {1,}).
Zero or more times (same as {0,}).
Zero or one time (same as {0,1}).
Page 1 | 2 | 3 | 4
Other PHP Tutorials
PHP Tutorial 1: Introduction to PHP
PHP Tutorial 2: Creating Simple PHP Programs
PHP Tutorial 3: String Processing and Regular Expressions (You are here)
PHP Tutorial 4: Form Processing and Business Logic