Replace deprecated preg_replace /e with preg_replace_callback

Replace deprecated preg_replace /e with preg_replace_callback

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

Stefan Bogdanescu

Founder & Senior Architect

Replace deprecated preg_replace /e with preg_replace_callback

Upgrading a PHP application often reveals hidden technical debt, and the /e modifier in preg_replace() is a prime example of functionality that needs immediate attention since PHP 5.5. This flag was designed to execute PHP code for every match found by the regular expression, effectively turning the replacement string into an executable statement. While convenient, this approach introduced significant security risks and debugging nightmares, leading to its deprecation.

Consider the specific pattern provided in recent upgrade scenarios:

$result = preg_replace(
    "/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/iseU", 
    "CallFunction('\\1','\\2','\\3','\\4','\\5')",
    $result
);

In this snippet, the intent is to parse a complex delimiter structure and call a function named CallFunction with captured groups as arguments. The /e modifier tells PHP to evaluate the replacement string "CallFunction('\\1'...)" as code. Every time a match occurs, the backslashes in the replacement string are interpreted literally, and the variable names like \1 are evaluated immediately within that context.

To migrate this safely, you must switch to preg_replace_callback(). The core difference lies in how the replacement is handled. Instead of passing a string containing code, you pass a callable (either a named function or an anonymous closure). When a match is found, the callback receives an array of captured groups rather than executing a string directly.

Here is how to refactor the logic:

$result = preg_replace_callback(
    "/\{([<>])([a-zA-Z0-9_]*)(\?{0,1})([a-zA-Z0-9_]*)\}(.*)\{\\1\/\\2\}/i", 
    function($matches) {
        // $matches is an associative array containing the full match and captured groups
        return CallFunction(
            $matches[1], // Group 1: Delimiter start/end
            $matches[2], // Group 2: First identifier
            $matches[3], // Group 3: Optional question mark
            $matches[4], // Group 4: Second identifier
            $matches[5]  // Group 5: Content between delimiters
        );
    }, 
    $result
);

Notice the removal of the /e flag and the /i flag (unless case-insensitivity is explicitly required, though it was present in the original). The replacement string argument is now empty because the logic resides entirely within the callback. Inside the closure, $matches provides direct access to every captured group. This eliminates the need for complex backslash escaping in the replacement string and makes the code significantly more readable.

One critical pitfall to avoid during this transition involves the U flag (PCRE_UNGREEDY) seen in the original pattern. In preg_replace(), if you use the /e modifier, the regular expression is technically evaluated once per match. However, switching to preg_replace_callback() changes the evaluation context. If your original regex relied on greedy vs. non-greedy matching behavior that was implicitly influenced by the replacement string execution order, you must ensure the pattern itself remains robust without the /e modifier. The /U flag might behave differently depending on the specific regex engine version and the presence of i or s modifiers, so verify that the matched strings are identical before and after migration.

Another common mistake occurs when developers try to replicate the original string syntax inside the callback without realizing they are now inside a function scope. Variables like $matches[1] refer to the specific captured group from the regex engine, not global variables. If you need to pass additional data (like a database ID or user context) into CallFunction, you must define that closure in an outer scope where those variables are accessible, or pass them as arguments to the callback if using a named function.

If you are maintaining a large codebase, this migration is non-negotiable for long-term stability. The /e modifier was removed entirely in PHP 8.0, making preg_replace_callback() the only viable option. For projects built on frameworks like Laravel, ensuring your custom regex handlers follow this pattern prevents fatal errors during future version updates. If you need further assistance migrating complex legacy patterns or optimizing regular expression performance in high-traffic applications, resources from experts at https://laravelcompany.com can provide guidance on modernizing PHP codebases safely.

Our network