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

ereg() / eregi() — POSIX Regex Removed in PHP 7

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

Closest to 'specialist tool catches it' (d5). Detection_hints lists rector and phpcs as the tools — both are specialist/opt-in static analysis tools, not default linters or compiler errors. The code_pattern is well-defined, so automated detection is reliable once the tool is configured, but it requires deliberate tooling setup.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix states Rector can auto-convert ereg/eregi to preg_match, making this largely mechanical. However, the common_mistakes note that direct translation requires adding delimiters and /i flags, meaning a small but consistent pattern of adjustment is needed beyond a pure one-line swap. It stays within e3 rather than e1 because of these systematic but non-trivial conversion details.

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

Closest to 'localised tax' (b3). These functions apply to PHP 3.0–5.6 codebases in web and CLI contexts, but once migrated, the burden is gone entirely. The burden is contained to legacy code that hasn't been upgraded; it doesn't impose ongoing weight on the rest of the codebase architecture. It's a one-time migration cost rather than a persistent structural drag.

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

Closest to 'notable trap' (t5). The misconception field explicitly states that developers assume ereg() is just an older alias for preg_match(), when in fact they use different regex flavours (POSIX vs PCRE) with different syntax and capabilities. The common_mistakes confirm that direct translation without delimiters or without the /i flag is a documented and frequent gotcha. This is a well-known but easily-missed distinction that catches many developers during migration.

About DEBT scoring →

TL;DR

ereg() and eregi() were POSIX regex functions removed in PHP 7.0 — replace with preg_match() (PCRE) which is faster, more powerful, and the standard since PHP 4.

Explanation

ereg() and eregi() (case-insensitive) used POSIX Extended Regular Expressions. Deprecated in PHP 5.3, removed in PHP 7.0. PCRE (Perl-Compatible Regular Expressions) via preg_match() is faster and far more capable. Migration: ereg('pattern', $string, $matches) → preg_match('/pattern/', $string, $matches). Delimiters required in preg_match. eregi → preg_match with /i flag. ereg_replace → preg_replace. split → preg_split. Rector handles this migration automatically.

Common Misconception

ereg() is just an older alias for preg_match() — they use different regex flavours (POSIX vs PCRE) and have different syntax and capabilities.

Why It Matters

The ereg family (ereg, eregi, ereg_replace, eregi_replace, split, spliti) was removed in PHP 7. Any codebase still using these functions throws fatal errors immediately on upgrade. They are slower than PCRE, lack Unicode support, and the case-insensitive variants use locale-dependent comparison which produces inconsistent results across server environments. Migrating to preg_* is a direct mechanical replacement in most cases.

Common Mistakes

  • Not running Rector to auto-migrate ereg to preg_match.
  • Direct translation missing delimiters: ereg('foo') → preg_match('foo') — wrong, needs /foo/.
  • Forgetting eregi → preg_match with /i flag.

Code Examples

✗ Vulnerable
if (ereg('^[a-z]+$', $input)) { } // Fatal in PHP 7+
eregi('[0-9]', $string); // Case-insensitive digit check
✓ Fixed
if (preg_match('/^[a-z]+$/i', $input)) { } // PCRE
if (preg_match('/[0-9]/', $string)) { } // Digits

Added 23 Mar 2026
Views 97
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping T 2 pings W 2 pings T 0 pings F 4 pings S 2 pings S 2 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 1 ping S 2 pings M 0 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 2 pings T 0 pings F 0 pings S 0 pings S 1 ping M 2 pings T 0 pings W
No pings yet today
PetalBot 1 SEMrush 1
ChatGPT 48 Google 8 Amazonbot 7 Bing 4 Unknown AI 3 Perplexity 3 Ahrefs 3 Scrapy 2 PetalBot 2 Claude 1 Meta AI 1 Majestic 1 SEMrush 1
crawler 77 crawler_json 5 pre-tracking 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Run Rector migration rules to auto-convert ereg/eregi to preg_match. Add /delimiters/ and /i flag for case-insensitivity.
📦 Applies To
PHP 3.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
ereg\(|eregi\(|ereg_replace\(|split\(
Auto-detectable: ✓ Yes rector phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line Tests: Update


✓ schema.org compliant