Download the PHP package liquidrazor/liquid-regex without Composer
On this page you can find all versions of the php package liquidrazor/liquid-regex. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download liquidrazor/liquid-regex
More information about liquidrazor/liquid-regex
Files in liquidrazor/liquid-regex
Package liquid-regex
Short Description An intuitive, simple and powerful Regex library for PHP.
License MIT
Informations about the package liquid-regex
LiquidRazor Regex
An intuitive, small, and fast regular expression helper for PHP 8.3+ with clean result objects and a few ergonomic utilities around PCRE.
Package:
liquidrazor/liquid-regexβ’ License: MIT
β¨ Features
- Fluent, tiny API for matching and replacing.
- Lazy, rewindable match results with
Iterator,ArrayAccess,Countable, andJsonSerializablesupport. - Deterministic result shapes via
ResultTypes(numeric, associative, or both) and aSTRICTfilter. - PCRE helpers to check version/JIT support and validate pattern syntax.
- Exceptions you can catch with informative messages bubbled from PCRE.
π§ Requirements
- PHP 8.3 or newer
- PCRE (bundled with PHP)
π¦ Installation
The library is namespaced under LiquidRazor\Regex and PSRβ4 autoloaded per composer.json.
π§ Concepts at a Glance
Regex β main entry point
The Regex class is the main faΓ§ade. It also memoizes compiled instances by pattern:
From there you can match or replace (see examples below).
ResultTypes β control your result shape
Choose how match arrays are exposed:
This lets you request exactly the view you need (e.g., only named groups). You can bitβOR flags:
Flags (PCRE capture flags)
Extra capture behavior forwarded to PCRE:
π Quick start
1) Find the first match
3) Replace
LiquidRazor\Regex\Result\Replace\Result implements Stringable. If the replacement result is an array (e.g., when replacing in arrays), __toString() returns JSON.
ποΈ Controlling result shapes
The LiquidRazor\Regex\Result\Matches\Result object exposes a lazy, memoized sequence of match entries. You decide which keys appear using ResultTypes:
INDEXED: keeps only integer keys (0,1,2β¦). WithSTRICT, filters out any non-integer keys.ASSOCIATIVE: keeps only named group keys. WithSTRICT, filters out numeric-looking strings.BOTH: includes both numeric and associative keys (default). Combine withSTRICTto prune mixed keys.
The object supports:
Iterator(rewindable)ArrayAccess(read-only; attempting to mutate throwsImmutableException)CountableJsonSerializable
If there was no match, the Result is empty with didMatch === false (safe to iterate/count/JSON-encode).
π§© PCRE helpers
Use the Pcre static utility for environment checks:
Note:
isValid()only checks a basic/.../flagsshape, also does compile the pattern and checks the result. If compilation fails, it returns false.
β οΈ Exceptions
The library centralizes PCRE errors through LiquidRazor\Regex\Exception classes:
PcreExceptionβ wrapspreg_last_error()& messageExceptionβ currently extendsPcreExceptionfor convenienceImmutableExceptionβ thrown on attempts to mutate read-only results
In practice, most API calls either return a Result object or throw on internal PCRE errors (e.g., bad backtrack limit).
π API sketch
The following reflects the current source structure at a high level. Some signatures may evolve.
Regex::compiled(string $pattern): RegexRegex->match(string $haystack, int $resultType = ResultTypes::BOTH, Flags ...$captureFlags): LiquidRazor\Regex\Result\Matches\ResultRegex->replace(string|array $subject, string|callable $replacement, int $limit = -1): LiquidRazor\Regex\Result\Replace\Result
Result types:
LiquidRazor\Regex\Result\Matches\Result- Properties:
pattern,haystack,didMatch - Interfaces:
Iterator,ArrayAccess(read-only),Countable,JsonSerializable
- Properties:
LiquidRazor\Regex\Result\Replace\Result implements Stringable- Properties:
pattern,count,replaced - Casting:
(string)$resultyields the final string (or JSON if array)
- Properties:
Helpers:
LiquidRazor\Regex\PcreversionString(): stringisJitSupported(): boolisValid(?string $pattern): bool
Enums / constants:
LiquidRazor\Regex\Lib\FlagsβDefault,OffsetCapture,UnmatchedAsNullLiquidRazor\Regex\Result\ResultTypesβINDEXED,ASSOCIATIVE,BOTH,STRICT
β Examples
Only named groups, strictly
With offsets and nulls for unmatched
π§ͺ Testing
You can plug this library into any test runner. The result objects are easy to snapshot by JSON encoding.
π€ Contributing
- Open an issue for design/API discussions.
- Keep the API minimal and predictable.
- PRs welcome with tests and docs for new behavior.
π License
MIT Β© LiquidRazor