Page 128 - Open Soource Technologies 304.indd
P. 128

Web Technologies-I



                   Notes         The behaviour of these character class specifications depends on your locale settings. By default,
                                 the classes are interpreted for the English language, however other interpretations can be achieved
                                 by calling setlocale( ).

                                                If you try to escape a character that does not need to be, such as an apostrophe,
                                                then the backslash will show up when you output the string.

                                 5.8.2 Regular Expression Functions

                                 PHP has several functions that use POSIX regular expressions to find and extract substrings,
                                 replace substrings, and split a string into an array. The functions to perform these tasks come in
                                 pairs: a case-sensitive version and a case-insensitive version.
                                 Finding and Extracting Values

                                 The ereg( ) function, and the case-insensitive version eregi( ), are defined as:
                                       boolean ereg(string pattern, string subject [, array var])
                                       boolean eregi(string pattern, string subject [, array var])

                                 Both functions return true if the regular expression pattern is found in the subject string. An
                                 optional array variable var can be passed as the third argument; it is populated with the portions
                                 of subject that are matched by up to nine grouped subexpressions in pattern. Subexpressions
                                 consist of characters enclosed in parentheses. Both functions return false if the pattern is not
                                 found in the subject.
                                 To extract values from a string into an array, patterns can be arranged in groups contained by
                                 parentheses in the regular expression. The following example shows how the year, month, and
                                 day components of a date can be extracted into an array:



                                 $parts = array( );
                                 $value = “2011-11-13”;

                                 $pattern = ‘^([0-9]{4})-([0-9]{2})-([0-9]{2})$’;
                                 ereg($pattern, $value, $parts);
                                 // Array ( [0] => 2011-11-13  [1] => 2011  [2] => 10  [3] => 13 )
                                 print_r($parts);
                                 The expression:
                                 ‘^([0-9]{4})-([0-9]{2})-([0-9]{2})$’
                                 matches dates in the format YYYY-MM-DD. After calling ereg( ), $parts[0] is assigned the portion
                                 of the string that matches the whole regular expression, in this case the whole string 2011-10-13.
                                 The portion of the date that matches each group in the expression is assigned to the following
                                 array elements: $parts [1] contains the year matched by ([0-9]{4}), $parts[2] contains the month
                                 matched by ([0-9]{2}), and $parts[3] contains the day matched by ([0-9]{2}).

                                 Replacing substrings
                                 The following functions create new strings by replacing substrings:
                                     string ereg_replace(string pattern, string replacement, string source)

                                     string eregi_replace(string pattern, string replacement, string source)




        122                               LOVELY PROFESSIONAL UNIVERSITY
   123   124   125   126   127   128   129   130   131   132   133