Comparison table of plain PHP and T‑Regx#

Here's a little table of some of the differences between the behaviour of plain PHP preg_match()preg_match_all()preg_replace()preg_split()preg_grep()preg_filter()preg_match() methods and T‑Regx API. This table intentionally shows the corner-cases and edge-cases, because that's the best way to illustrate differences between T‑Regx and vanilla-PHP.

SafeRegex compared to PHP#

Table presents the most important differences in error handling and general maturity between T‑Regx preg::match()preg::match_all()preg::replace()preg::split()preg::grep()preg::filter()preg::match() methods and PHP preg_match()preg_match_all()preg_replace()preg_split()preg_grep()preg_filter()preg_match() methods.

Using na invalid pattern(eg. /unclosed[A-/)
T-Regx throws MalformedPatternExceptionwith descriptive message
Corrupted subject(eg. malformed utf-8 sequence)
T-Regx throws RuntimeCleanRegexException with descriptive message
Using an overly complex pattern(eg. containing ?R)
T-Regx throws RuntimeCleanRegexException with descriptive message
Returning an invalid replacement value
  • preg_last_error() returns success code(returns PREG_NO_ERROR)
  • Depending on the value, PHP:
    • Silently converts the value to string(for example for int or "1"/"0" for bool)
    • Raises a warning (for example for array)
    • Throws a fatal error, terminating the application(for example for stdClass or objects without __toString())`
T-Regx throws InvalidReplacementException with descriptive message

CleanRegex compared PHP#

This table shows difference between higher-level CleanRegex API pattern() and SafeRegex preg::match()preg::match_all()preg::replace()preg::split()preg::grep()preg::filter()preg::match() methods (which share the same interface as preg_match()preg_match_all()preg_replace()preg_split()preg_grep()preg_filter()preg_match()).

Using an invalid capturing group name(eg. name "!@#$" or index -2)
Actually tries to get the groupDepending on PHP settings, either returns null and/or issues a warning
T-Regx throws \InvalidArgumentException with descriptive message
Using a nonexistent group(group name typo, group deleted)
Actually tries to get the groupDepending on PHP settings, either returns null and/or issues a warning
T-Regx throws NonexistentGroupException with descriptive message
Using an unmatched optional group(conditional group, unmatched by subject)
Actually tries to get the groupDepending on PHP settings, either returns null and/or issues a warning
T-Regx throws GroupNotMatchedException with descriptive message
Offsets in UTF-8, for example for "18€"
5 bytesPHP offsets are in bytes
  • 3 charactersWith method offset()
  • 5 bytesWith method byteOffset()
Offset while replacing
PHP doesn't keep offsets while replacing
With T-Regx, you can use offset() or modifiedOffset()
Worst case complexity

(string|int|null)[][][]

array of arrays of arrays of string/null and intpreg_match_all() with PREG_OFFSET_CAPTURE
string[]array of strings
Possible outcomes when testing a subject
  • 1/0 - matches or differs
  • false - for certain errors
  • issues a warning - for other errors
  • true/false - matches or differs
  • MalformedPatternException - malformed pattern