![]() Back to www.deitel.com |
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.8: expression.php -->
|
|
5 <!-- Using regular expressions -->
|
|
6
|
|
7 <html xmlns = "http://www.w3.org/1999/xhtml">
|
|
8 <head>
|
|
9 <title>Regular expressions</title>
|
|
10 </head>
|
|
11
|
|
12 <body>
|
|
13 <?php
|
|
14 $search = "Now is the time";
|
|
15 print( "Test string is: '$search'<br /><br />" );
|
|
16
|
|
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 />" );
|
|
21
|
|
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 />" );
|
|
27
|
|
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 />" );
|
|
32
|
|
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 />" );
|
|
38
|
|
39 // search for any words beginning with 't'
|
|
40 print( "Words beginning with 't' found: ");
|
|
41
|
|
42 while ( eregi( "[[:<:]](t[[:alpha:]]+)[[:>:]]",
|
|
43 $search, $match ) ) {
|
|
44 print( $match[ 1 ] . " " );
|
|
45
|
|
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 );
|
|
49
|
|
50
|
|
51 print( "<br />" );
|
|
52 ?>
|
|
53 </body>
|
|
54 </html>
|
|
|