← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

preg_replace /e Modifier (Removed)

Security PHP 3.0+ Advanced
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), semgrep and phpstan rules flag the /e modifier pattern reliably; also PHP 7+ removes it entirely making it a fatal error there.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), quick_fix is to swap preg_replace with /e for preg_replace_callback — a localized pattern replacement per call site, though it requires auditing all occurrences.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), affects only the regex call sites in legacy code; not architectural but requires ongoing audit across the codebase wherever preg_replace is used.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7), misconception states devs think /e evaluates simple expressions but it executes arbitrary PHP code including system()/exec() — the modifier letter gives no hint of code execution semantics.

About DEBT scoring →

TL;DR

The /e modifier in preg_replace() evaluated the replacement as PHP code — removed in PHP 7.0. Any legacy code using it is a critical RCE vulnerability.

Explanation

preg_replace($pattern . 'e', $replacement, $subject) evaluated $replacement as PHP code after substitution. This allowed: preg_replace('/.*/e', $_GET['cmd'], '') — direct remote code execution from user input. Removed in PHP 7.0 (was deprecated in PHP 5.5). The replacement is preg_replace_callback() with an explicit closure. Any legacy codebase running on PHP 5 with user input touching preg_replace with the /e flag has a critical RCE. Check all preg_replace calls for the 'e' flag in regex patterns.

Common Misconception

The /e modifier only evaluates simple expressions — it evaluates full PHP code including system(), exec(), and arbitrary function calls.

Why It Matters

preg_replace /e with user-controlled input or replacement is a direct remote code execution vulnerability — one of the most critical PHP security issues in legacy code.

Common Mistakes

  • Any use of preg_replace with /e flag from user input.
  • Not auditing all preg_replace calls in legacy codebases.
  • Using variable patterns: preg_replace($userPattern . 'e', ...).

Code Examples

✗ Vulnerable
// PHP 5 — critical RCE:
preg_replace('/' . $_GET['pattern'] . '/e',
             $_GET['replacement'],
             $subject);
// Attacker: ?pattern=.*&replacement=system('cat /etc/passwd')
✓ Fixed
// PHP 7+ — use preg_replace_callback:
$result = preg_replace_callback(
    '/([a-z]+)/',
    function(array $matches): string {
        return strtoupper($matches[1]);
    },
    $subject
);
// Never pass user input as the callback

Added 22 Mar 2026
Views 178
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 1 ping W 2 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 2 pings S 0 pings S 4 pings M 2 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
ChatGPT 128 Google 7 Amazonbot 7 Perplexity 7 Scrapy 5 SEMrush 4 Unknown AI 3 Ahrefs 3 Meta AI 2 Claude 2 Bing 1 PetalBot 1
crawler 162 crawler_json 6 pre-tracking 2
DEV INTEL Tools & Severity
🔴 Critical ⚙ Fix effort: Medium
⚡ Quick Fix
Replace all preg_replace with /e flag with preg_replace_callback. Upgrade to PHP 7+. Audit ALL preg_replace calls in legacy code for /e flag.
📦 Applies To
PHP 3.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
preg_replace.*['"].*e['"]
Auto-detectable: ✓ Yes semgrep phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Medium Context: Function Tests: Update
CWE-94 CWE-74 CWE-95


✓ schema.org compliant