PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead

PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP7 - The /e modifier is no longer supported, use preg_replace_callback instead

If you are deploying a PHP application on version 7 or later, encountering the warning Warning: preg_replace(): The /e modifier is no longer supported is an expected part of the transition. This error arises because the /e (evaluate) flag was removed from the regular expression engine to address significant security vulnerabilities and performance issues associated with dynamic code execution within regex patterns.

Your original implementation relied on $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));. The logic here attempted to replace a character following a line start, tab, or hyphen with the uppercase version of that character. While this worked in PHP 5.x, the syntax preg_replace('/pattern/e', 'code', $subject) is functionally identical to calling eval() on your replacement string for every match found. This created a massive attack surface where malicious input could alter the flow of your application logic simply by matching against the pattern.

The correct migration path is to use preg_replace_callback. However, the error message you are seeing—Warning: preg_replace_callback(): The /e modifier is no longer supported—suggests that the /e flag might still be present in your regex pattern definition or that the function call itself is not being interpreted correctly. In PHP 7+, the /e flag is simply ignored and will throw a warning if included in the pattern string passed to preg_replace.

To resolve this, you must remove the /e from the delimiter flags and provide a callback function instead. Your second attempt showed a good start, but there is a critical logical error in how the replacement was handled:

$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./', 
    function ($matches) {
        foreach ($matches as $match) {
            return strtoupper($match);
        }
    }, 
    strtolower(trim($match[1]))
);

The issue lies inside the callback function. You are iterating over $matches and returning strtoupper($match) inside the loop. Because a return statement exits the function immediately, only the first match is processed, and the rest of the string remains unchanged. Furthermore, you are returning the value rather than modifying the replacement buffer, which preg_replace_callback expects to receive as a single string representing the entire replacement.

The correct approach involves capturing the specific character that needs conversion and returning its uppercase equivalent directly from the closure. Here is how to fix the logic:

$subject = strtolower(trim($match[1]));
$pattern = '/(?<=^|[\x09\x20\x2D])./'; // No /e flag here

$result = preg_replace_callback($pattern, function ($matches) use (&$result) {
    $char = $matches[0];
    return strtoupper($char);
}, $subject);

$match[1] = $result;

In this corrected version, the /e flag is completely removed from the pattern. The callback receives an array where $matches[0] contains the matched character. By returning strtoupper($char) directly without a loop, you ensure that every matched character is converted to uppercase.

If your codebase contains many instances of the old /e syntax, such as complex eval-based replacements or obfuscated logic, manually refactoring them can be tedious. For large-scale migrations, it is often practical to use preg_replace_callback with a custom wrapper function that strips the /e flag programmatically before execution, though direct replacement is preferred for clarity and security.

For developers maintaining legacy PHP 5 codebases or working in environments where strict error reporting hides these warnings, updating to preg_replace_callback is non-negotiable for PHP 7+ compatibility. If you are building a new project on the Laravel framework, ensure your regex utilities follow this standard to avoid unexpected deprecation notices. For more details on handling legacy patterns in modern stacks, refer to resources like https://laravelcompany.com which often provide best practices for upgrading older codebases.

Our network