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)
Placing a pattern in parentheses stores the matched string in the array that is specified in the third argument to function ereg. The first parenthetical pattern matched is stored in the second array element, the second in the third array element and so on. The first element (i.e., index 0) stores the string matched for the entire pattern. The parentheses in lines 34-35 result in Now being stored in variable $match[ 1 ].
Searching for multiple instances of a pattern in a string is slightly more complicated, because the ereg function matches only the first instance of the pattern. To find multiple instances of a given pattern, we must remove any matched instances before calling ereg again. Lines 42-49 use a while loop and the ereg_replace function to find all the words in the string that begins with t. We will say more about this function momentarily.
The pattern used in this example, [[:<:]](t[[:alpha:]]+)[[:>:]], matches any word beginning with the character t followed by one or more characters. The example uses the character class [[:alpha:]] to recognize any alphabetic character. This is equivalent to the [a-zA-Z] bracket expression that was used earlier. Figure 26.10 lists some character classes that can be matched with regular expressions.
Fig. 26.10 Some PHP character classes.
Character class
Alphanumeric characters (i.e., letters [a-zA-Z] or digits [0-9]).
Word characters (i.e., letters [a-zA-Z]).
Lowercase letters.
Uppercase letters.

The quantifier + matches one or more instances of the preceding expression. The result of the match is stored in $match[ 1 ]. Once a match is found, we print it in line 44. We then remove it from the string in line 48, using function ereg_replace. Function ereg_replace takes three arguments-the pattern to match, a string to replace the matched string and the string to search. The modified string is returned. Here, we search for the word that we matched with the regular expression, replace the word with an empty string, then assign the result back to $search. This allows us to match any other words beginning with the character t in the string.
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