The POSIX regular expression extension is enabled in PHP by default, unless explicitly disabled by specifying the --disable-regex switch to the configuration program before compiling it. The one trick to this is that the extension only supports multi-byte strings if you also enable the mbstring extension. Microsoft Windows users merely need to make sure the following line in php.ini has no semicolon ( ; ) character in front of it:


You have no configuration options for this extension in php.ini, so after it is compiled or enabled, it is ready for use.

Testing Your Expressions

The next few sections cover the specifics of regular expression syntax, but first you learn how to test and play with the functionality in PHP5. Although we could use POSIX regular expressions with a number of functions, we limit ourselves initially to the ereg function, which takes a string and a regular expression and tells whether the string matches the pattern and, if so, what exactly the match was:

$success = ereg($pattern, $string, $match);

The third parameter is optional and can be omitted when you are interested strictly in whether a match occurred. When it is specified and a match is found, an array with those match(es) is placed into this variable.

Most Unix-like systems (including Mac OS X) also ship with a program called egrep, which applies the regular expression to each line in an input file individually, indicating which lines match and which ones do not:

# egrep [options] pattern  files

To have the ability to process a number of input lines in PHP and indicate which ones match, we can write our own function, which we will call the_regex and use while we explore the functionality:

function the_regex($in_strings, $in_regex)
  if (!is_array($in_strings) || !is_string($in_regex))
    die('Bad Parameters (array + string)<br/>');
  echo <<<EOM
<b>the_regex</b> called to match <b>'{$in_regex}'</b>:<br/>
  foreach ($in_strings as $x => $strval)
    $found = ereg($in_regex, $strval, $matches);
    if ($found)
      echo "Array Index <b>$x</b> matches: ";
      var_export($matches); echo " \"$strval\"<br/>\n";
  echo "<br/>\n";

This function merely takes an array of strings and the regular expression to match and indicates which of the strings in the array matches the pattern. As we will see, regular expressions use many of the same characters that PHP uses for special string processing. Therefore, it is almost always in the best interest to enclose regular expression patterns in single quotes (') rather than double quotes (").

by BrainBellupdated