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)
Lines 23 and 25 call function strcmp to compare two strings. If the first string alphabetically precedes the second string, then -1 is returned. If the strings are equal, then 0 is returned. If the first string alphabetically follows the second string, then 1 is returned. Lines 23-29 compare each element to the string "banana", printing the elements that are greater than, less than and equal to the string.
Relational operators (==, !=, <, <=, > and >=) can also be used to compare strings. Lines 33-38 use relational operators to compare each element of the array to the string "apple". These operators are also used for numerical comparison with integers and doubles.
Using Regular Expressions
For more powerful string comparisons, PHP provides functions ereg and preg_match, which use regular expressions to search a string for a specified pattern. Function ereg uses Portable Operating System Interface (POSIX) extended regular expressions, whereas function preg_match provides Perl-compatible regular expressions. POSIX-extended regular expressions are a standard to which PHP regular expressions conform. In this section, we use function ereg. Perl regular expressions are more widely used than POSIX regular expressions. Support for Perl regular expressions also eases migration from Perl to PHP. For more information on Perl regular expressions, see Chapter 25. Consult PHP's documentation for a list of differences between the Perl and PHP implementations. Figure 26.8 demonstrates some of PHP's regular expression capabilities.
We begin by assigning the string "Now is the time" to variable $search (line 14). Line 19's condition calls function ereg to search for the literal characters Now inside variable $search. If the pattern is found, ereg returns true, and line 20 prints a message indicating that the pattern was found. We use single quotes ('') inside the print statement to emphasize the search pattern. Anything enclosed within single quotes is not interpolated. For example, '$name' in a print statement would output $name, not variable $name's value. Function ereg takes two arguments-a regular expression pattern to search for (Now) and the string to search. Although case mixture and whitespace are typically significant in patterns, PHP provides function eregi for specifying case-insensitive pattern matches.
Fig. 26.8 Regular expressions in PHP. (Part 1 of 2.)
1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2      "">
4   <!-- Fig. 26.8: expression.php -->
5   <!-- Using regular expressions -->
7   <html xmlns = "">
8       <head>
9          <title>Regular expressions</title>
10       </head>
12       <body>
13         <?php
14            $search = "Now is the time";
15             print( "Test string is: '$search'<br /><br />" );
17             // call function ereg to search for pattern 'Now'
18             // in variable search
19             if ( ereg( "Now", $search ) )
20                print( "String 'Now' was found.<br />" );
22             // search for pattern 'Now' in the beginning of
23             // the string
24             if ( ereg( "^Now", $search ) )
25                print( "String 'Now' found at beginning
26                  of the line.<br />" );
28             // search for pattern 'Now' at the end of the string
29             if ( ereg( "Now$", $search ) )
30   v          print( "String 'Now' was found at the end
31               of the line.<br />" );
33            // search for any word ending in 'ow'
34             if ( ereg( "[[:<:]]([a-zA-Z]*ow)[[:>:]]", $search,
35               $match ) )
36               print( "Word found ending in 'ow': " .
37                  $match[ 1 ] . "<br />" );
39             // search for any words beginning with 't'
40             print( "Words beginning with 't' found: ");
42             while ( eregi( "[[:<:]](t[[:alpha:]]+)[[:>:]]",
43               $search, $match ) ) {
44                print( $match[ 1 ] . " " );
46                // remove the first occurrence of a word beginning
47                // with 't' to find other instances in the string
48               $search = ereg_replace( $match[ 1 ], "", $search );
51             print( "<br />" );
52         ?>
53       </body>
54   </html>


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