composer/pcre
Type-safe wrappers around PHP's preg_* functions that fail loudly instead of returning unexpected nulls. Enforces PREG_UNMATCHED_AS_NULL, simplifies return types for PHPStan and Psalm, and includes a PHPStan extension for better regex analysis.
preg_match/preg_replace call that can return null or false is an unhandled branch. Adopting composer/pcre converts those into PcreExceptions you can catch at a boundary (request validation, job handler, importer), decide on, and log. This is a tractable, low-risk reliability epic that maps to a measurable goal: reduce preg_*-related production errors and undefined-index warnings to zero.RegexHelper or SafeRegex class. This package is that class, maintained by the Composer maintainers, MIT-licensed, with a stable API since 2.0. Decision: buy. Spend the engineering budget on product work instead of maintaining a wrapper.vendor/composer/pcre/extension.neon) infers precise $matches array shapes from the regex literal and validates the pattern itself, plus subject narrowing (3.4.0+, PHPStan 2.2.2+). This directly unlocks raising the static-analysis baseline — a prerequisite for safe refactors in a large Laravel codebase and a durable velocity win rather than a one-off fix.null from replace today quietly writes "null" into stored data.*StrictGroups variants let you declare "this pattern's groups are always present," turning optional-capture-group bugs into loud, early exceptions and removing isset()/!== '' defensive code. That is an enforceable coding guideline, not just a library choice.Adopt when:
null/false return handling, isset($matches[3]), TypeErrors from array offsets, or PHPStan/Psalm suppressions around preg_*.try/catch (PcreException) at sensible boundaries. This is an architectural preference worth validating with the team before committing.null when unmatched) — the library always sets PREG_UNMATCHED_AS_NULL, which removes the "empty string vs. no match" ambiguity.composer.lock shows it is already present transitively; incremental cost is then effectively zero.Look elsewhere or scope down when:
rawr/t-regx is the explicit alternative the README points to.preg_filter, array $subject for replace*, PREG_SET_ORDER in matchAll, and offset flags mixed into the generic match/matchAll/split signatures.For executives (one paragraph): We can eliminate a recurring class of production bugs — regex operations that fail silently and write empty or null data — at essentially no cost. composer/pcre is a small, MIT-licensed, actively maintained wrapper around PHP's regex functions, maintained by the people who build Composer itself. It replaces silent failures with catchable exceptions and improves our static-analysis coverage, which reduces defect escape rate and speeds up refactoring across the whole codebase. It is a library swap, not a product change: no customer-visible behavior changes, no new infrastructure, days not quarters. Success is measurable: zero preg_*-attributed errors in production and a higher PHPStan level enforced in CI.
For engineering leadership: Thin, drop-in replacement for preg_* via Preg:: and Regex:: static APIs; always applies PREG_UNMATCHED_AS_NULL; throws Composer\Pcre\PcreException on failure; *StrictGroups variants for patterns with guaranteed groups; result objects (MatchResult, ReplaceResult) as an alternative to by-ref $matches. Ships a PHPStan extension for precise match types and regex validation. Known, documented limitations (no preg_filter, no array subjects in replace*, no PREG_SET_ORDER) mean we adopt it with a written migration checklist and a code-style rule rather than a blanket find-and-replace.
For the team: Migration is mechanical and reviewable — swap preg_* for Preg::*, add try/catch (or a helper that maps PcreException to a domain error) where failure is a real product state, enable extension.neon in PHPStan, and pin the major version compatible with our PHP floor. Track progress by call sites migrated and by a decrease in defensive null/false checks.
How can I help you explore Laravel packages today?