Team LiB
Previous Section Next Section

PCRE Modifiers

Remember when I mentioned that you need delimiters to specify a PCRE? If you were wondering why, here's an explanation. PCRE introduces the concept of "modifiers" that can be appended to a regular expression to alter the behavior of the regex compiler and/or interpreter. A modifier is always appended at the end of an expression, right after the delimiter. For example, in the following regex:

/test/i

the last i is a modifier.

There are many different modifiers. Perhaps the most commonly used one is i, which renders the regular expression non case sensitive. Here's an example of how it works:

<?php

    $s = 'Another beautiful day';

    echo (preg_match ('/BEautiFul/i', $s) ? 'MATCH' : 'NO MATCH') . "\n";

?>

If you execute the preceding script, it will output the word "MATCH", indicating that the regex succeeded because the i modifier made it not case sensitive.

Another commonly usedand extremely powerfulmodifier is e, which, used in conjunction with i modifier, causes the regex compiler to interpret the replacement parameter not as a simple string but as a PHP expression that is executed and whose result is used as the replacement string.

Here's an example that shows you just how powerful this modifier is:

<?php

    $a = array
    (
      'name'  =>  'Toronto',
      'object'=>  'town'
    );

    $s = '{name} is a really cool {object}';

    echo preg_replace ('/{(\w+)}/e', '$a["\1"]', $s);

?>

When you execute this script, preg_replace() finds all instances of alphanumeric strings delimited by { and }, replaces the reference they create in $a["\1"], executes the resulting PHP expression, and replaces its value in the original string.

Let's make a step-by-step example. The first match in the regex will be the substring name, which is then placed in the replacement string, thus providing the PHP expression $a["name"]. The latter, when executed, returns the value Toronto, which is then substituted inside the original string. The same process is repeated for the second match object, and the final result is then returned:

Toronto is a really cool town

Imagine how much more complex doing something like this would have been without regular expressions and the e modifier!

    Team LiB
    Previous Section Next Section