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:
preg_replace()
: modifier deprecated in PHP 5.5 and removed in PHP 7.0.preg_filter()
: modifier deprecated in PHP 5.5 and removed in PHP 7.0.mb_ereg_replace()
: modifier deprecated in PHP 7.1 and removed in PHP 8.0 (also applies to aliasesmbereg_replace()
andmberegi_replace()
).mb_eregi_replace()
: modifier deprecated in PHP 7.1 and removed in PHP 8.0.mb_regex_set_options()
: modifier deprecated in PHP 7.1 and removed in PHP 8.0 (but it was never supported via this function in the first place).
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
mb_regex_set_options('e');
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:
mb_regex_set_options('');
Errors or Warnings
- The /e modifier is no longer supported, use preg_replace_callback instead
- The /e modifier is deprecated, use preg_replace_callback instead
- Uncaught ValueError: Option "e" is not supported
- The 'e' option is deprecated, use mb_ereg_replace_callback instead