XLVI. Regular expression functions

Regular expressions are used for complex string manipulation in PHP. The functions that support regular expressions are:

These functions all take a regular expression string as their first argument. PHP uses the POSIX extended regular expressions as defined by POSIX 1003.2. For a full description of POSIX regular expressions see the regex man pages included in the regex directory in the PHP distribution. It's in manpage format, so you'll want to do something along the lines of man /usr/local/src/regex/regex.7 in order to read it.

Example 1. Regular expression examples

  1 
  2 ereg("abc",$string);            
  3 /* Returns true if "abc"
  4    is found anywhere in $string. */
  5 
  6 ereg("^abc",$string);
  7 /* Returns true if "abc"
  8    is found at the beginning of $string. */
  9 
 10 ereg("abc$",$string);
 11 /* Returns true if "abc"
 12    is found at the end of $string. */
 13 
 14 eregi("(ozilla.[23]|MSIE.3)",$HTTP_USER_AGENT);  
 15 /* Returns true if client browser
 16    is Netscape 2, 3 or MSIE 3. */
 17 
 18 ereg("([[:alnum:]]+) ([[:alnum:]]+) ([[:alnum:]]+)",
 19      $string,$regs); 
 20 /* Places three space separated words
 21    into $regs[1], $regs[2] and $regs[3]. */
 22 
 23 $string = ereg_replace("^","<BR>",$string); 
 24 /* Put a <BR> tag at the beginning of $string. */
 25  
 26 $string = ereg_replace("$","<BR>",$string); 
 27 /* Put a <BR> tag at the end of $string. */
 28 
 29 $string = ereg_replace("\n","",$string);
 30 /* Get rid of any newline
 31    characters in $string. */
 32      

Table of Contents
ereg — regular expression match
ereg_replace — replace regular expression
eregi — case insensitive regular expression match
eregi_replace — replace regular expression case insensitive
split — split string into array by regular expression
sql_regcase — make regular expression for case insensitive match