Deitel & Associates, Inc. Logo

Back to www.deitel.com
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

ISBN:
0-13-145091-3
© 2004
pages: 1420

Order
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
PHP processes text data easily and efficiently, enabling straightforward searching, substitution, extraction and concatenation of strings. Text manipulation in PHP is usually done with regular expressions—a series of characters that serve as pattern-matching templates (or search criteria) in strings, text files and databases. This feature allows complex searching and string processing to be performed using relatively simple expressions.
Comparing Strings
Many string-processing tasks are accomplished by using PHP's equality and comparison operators (Fig. 26.7). Line 16 declares and initializes array $fruits by calling function array. The for loop (line 19) iterates through each element in the $fruits array.
Fig. 26.7 Using the string comparison operators. (Part 1 of 2.)
1   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3   
4   <!-- Fig. 26.7: compare.php -->
5   <!-- String Comparison -->
6   
7   <html xmlns = "http://www.w3.org/1999/xhtml">
8       <head>
9           <title>String Comparison</title>
10       </head>
11   
12       <body>
13         <?php
14   
15            // create array fruits
16            $fruits = array( "apple", "orange", "banana" );
17   
18            // iterate through each array element
19            for ( $i = 0; $i < count( $fruits ); $i++ ) {
20   
21             // call function strcmp to compare the array element
22             // to string "banana"
23            if ( strcmp( $fruits[ $i ], "banana" ) < 0 )
24               print( $fruits[ $i ]." is less than banana " );
25            elseif ( strcmp( $fruits[ $i ], "banana" ) > 0 )
26               print( $fruits[ $i ].
27                   " is greater than banana " );
28            else
29               print( $fruits[ $i ]." is equal to banana " );
30   
31             // use relational operators to compare each element
32             // to string "apple"
33            if ( $fruits[ $i ] < "apple" )
34                print( "and less than apple! <br />" );
35            elseif ( $fruits[ $i ] > "apple" )
36                print( "and greater than apple! <br />" );
37            elseif ( $fruits[ $i ] == "apple" )
38                print( "and equal to apple! <br />" );
39   
40            }
41         ?>
42       </body>
43   </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