Warning: preg_match(): Backtrack limit was exhausted in /var/www/app/src/Parser/HtmlParser.php on line 67
ID: php/pcre-backtrack-limit
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 83 | active | — | — | — |
Root Cause
PCRE backtrack limit errors occur when a regular expression requires more backtracking steps than allowed by pcre.backtrack_limit (default 1,000,000). This usually indicates a catastrophic backtracking pattern that needs to be rewritten, not a limit that should be raised.
genericWorkarounds
-
88% success Rewrite the regex to avoid catastrophic backtracking with atomic groups or possessive quantifiers
Replace patterns like (a+)+ with (?>a+)+ (atomic group) or a++ (possessive quantifier). Use specific character classes instead of greedy .* patterns. For example, replace '".*"' with '"[^"]*"' to avoid backtracking on quoted strings.
-
85% success Use a dedicated parser instead of regex for complex structured text
For HTML parsing, use DOMDocument or a library like symfony/dom-crawler instead of regex. For JSON, use json_decode(). For CSV, use fgetcsv(). Dedicated parsers handle edge cases correctly without backtracking issues.
Dead Ends
Common approaches that don't work:
-
Increasing pcre.backtrack_limit to a very large value
75% fail
The backtracking pattern has exponential time complexity. Raising the limit from 1M to 10M only allows it to run 10x longer before failing. On slightly larger input, it will hit the new limit and also consume excessive CPU.
-
Suppressing the PCRE error with @ operator and treating no-match as valid
80% fail
When preg_match() returns false (error) instead of 0 (no match), the result is fundamentally different. Suppressing this silently skips validation or parsing that the regex was meant to perform.