Regex "e" Modifier Deprecated and Removed

The e modifier, used for evaluating PHP code on matches of regular expressions, was deprecated and eventually removed on multiple functions:

Here is an example with preg_replace() that would capitalize all occurrences of the letter h:

preg_replace('/(h)/e', 'strtoupper("\\1")', 'hello there');

After removal, preg_*() functions emit a warning, but mb_*() functions throw a ValueError.

Solution: preg_replace

Use functions that allow for explicit callbacks. This code is supported on PHP 5.3 and higher:

preg_replace_callback('/(h)/', function ($matches) { 
    return strtoupper($matches[1]);
}, 'hello there');

This code is supported on PHP 5.2 and lower, in case you want to upgrade your code over time instead of all at once.

preg_replace_callback('/(h)/', 'capitalizeH', 'hello there');

function capitalizeH($matches) {
    return strtoupper($matches[1]);

Solution: preg_filter

preg_filter('/(h)/e', 'strtoupper("\\1")', array('hello', 'friend'));

Use preg_filter() to first obtain the list of subjects matching the pattern, then transform using preg_replace_callback() and the same pattern:

$matchingSubjects = preg_filter('/(h)/', '$1', array('hello', 'friend'));
preg_replace_callback('/(h)/', function ($matches) {
    return strtoupper($matches[1]);
}, $matchingSubjects);
// Will result in array('hellO'), the second subject having been
// filtered out in the first statement.

Solution: mb_ereg_replace

mb_ereg_replace('(h)', 'strtoupper("\\1")', 'hello there', 'e');

Use mb_ereg_replace_callback(). This function is only available since PHP 5.4.1.

mb_ereg_replace_callback('(h)', function ($matches) { 
    return strtoupper($matches[1]);
}, 'hello there');

Solution: mb_eregi_replace

mb_eregi_replace('(H)', 'strtoupper("\\1")', 'hello there', 'e');

Use mb_ereg_replace_callback() with the i option to allow case-insensitive searches. This function is only available since PHP 5.4.1.

mb_ereg_replace_callback('(H)', function ($matches) { 
    return strtoupper($matches[1]);
}, 'hello there', 'i');

Solution: mb_regex_set_options


This modifier never had any effect via this function. It only had an effect via the replace functions, so you can safely remove it from mb_regex_set_options() calls:


Errors or Warnings

See Also