14.2 POSIX Style


In addition to Perl-style regular expressions, PHP supports POSIX-style regular expressions. Depending on which style you like best, you can decide whether you want to use Perl-style or POSIX-style regular expressions.

PHP supports a set of functions related to regular expressions:

  • ereg Looks for matches

  • ereg_replac Performs substitutions

  • eregi Performs case-insensitive matching

  • eregi_replac Performs substitutions based on case-insensitive matching

  • split Splits a string based on regular expressions

  • spliti Splits a string based on regular expressions based on case-insensitive matching

  • sql_regcase Creates a regular expression matching the string passed to the function

After you have seen which functions are provided by PHP, you will see how these functions can be used.

14.2.1 ereg

If a substring matches a regular expression, true is returned. In POSIX-style regular expressions, no slashes are needed to mark the beginning and the end of a regular expression, as you can see in the next example:

 <?php         $string = "Regular expressions are cool";         if      (ereg("cool", $string))         {                 echo "Yeah, regular expressions are cool<br>";         } ?> 

The regular expression matches the string, so the echo command will be executed.

In many cases it can be useful to retrieve the substrings matching the regular expression. Therefore, you must define a parameter defining the name of the array, which you want the matching substrings to be stored in. This way it is possible to find out how many components have been retrieved from the string:

 <?php         $string = "Regular expressions are cool";         if      (ereg("cool", $string, $regs))         {                 echo "Yeah, regular expressions are cool<br>";                 echo "Matches: $regs[0]<br>";         } ?> 

In this scenario only one word matches the regular expression:

 Yeah, regular expressions are cool Matches: cool 

As you can see, the result is stored in the array defined by the third parameter of the ereg function.

14.2.2 ereg_replace

Searching for substrings is not enough. The power of regular expressions lies in the ability to change strings satisfying an expression. With the help of the ereg_replace command, substitutions can be performed:

 <?php         $string = "Regular expressions are cool";         $string = ereg_replace("cool", "great", $string);         echo "String: $string<br>"; ?> 

When you execute the script, one line will be displayed:

 String: Regular expressions are great 

14.2.3 eregi

The eregi function does the same sort of searching as the ereg function, but eregi performs case-insensitive searching instead of case-sensitive searching.

The next example shows how you can find out if $string contains the word BEER:

 <?php         $string = "Epi is looking for free beer";         if      (eregi("BE{2}R", $string))         {                 echo "Epi seems to like beer ...<br>";         } ?> 

Although the word BEER is spelled in uppercase letters, the string matches the regular expression. If the script is executed, you will find out that Epi seems to like beer.

 Epi seems to like beer ... 

14.2.4 eregi_replace

The eregi_replace function is the counterpart of the ereg_replace function. In contrast to the ereg_replace function, eregi_replace performs case-insensitive searching.

Let's take a look at an example:

 <?php         $string = "Epi is looking for free beer";         $string = eregi_replace("ep+i", "Hans", $string);         echo "$string<br>"; ?> 

In the string processed by the regular expression, Epi has been spelled in uppercase letters. In the regular expression, the E in Epi is spelled in lowercase letters. However, $string matches the regular expression because eregi_replace performs case-insensitive pattern matching.

14.2.5 sql_regcase

To generate a regular expression matching a string, you can use the function sql_regcase. The regular expression generated by the function performs case- insensitive matching and can be used directly by calling almost any other function related to POSIX-style regular expressions. The next example shows how a string can be transformed into a regular expression matching the string itself:

 <?php         $string = "check out www.postgreql.at";         $result = sql_regcase($string);         echo "result: $result<br>\n"; ?> 

sql_regcase is called with just one parameter. Let's see what comes out when the script is executed:

 result: [Cc][Hh][Ee][Cc][Kk] [Oo][Uu][Tt] [Ww][Ww][Ww].[Pp][Oo][Ss][Tt][Gg] [Rr][Ee][Qq][Ll].[Aa][Tt] 

As you can see, a simple regular expression has been generated.



PHP and PostgreSQL. Advanced Web Programming2002
PHP and PostgreSQL. Advanced Web Programming2002
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 201

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net