Hi all,
I'd like to propose adding a PREG_THROW_ON_ERROR flag to the preg_*()
functions, and gauge interest in that.
Json and ext/filter both grew an opt-in way to turn a silent error into
an exception...that is JSON_THROW_ON_ERROR and FILTER_THROW_ON_FAILURE.
PCRE is the one left out where a failing call is easy to miss. An execution
error for example (e.g. a bad UTF-8) surfaces only if
you call preg_last_error() afterwards, and a malformed pattern emits a
warning and returns false.
PREG_THROW_ON_ERROR would do for PCRE what those two do for their
functions.
Passing it to any preg_*() call makes a PCRE error throw a
Pcre\PcreException that carries the PREG_*_ERROR code and the
preg_last_error_msg() text
instead of warning or returning false/null.
And it covers both kinds: a compilation error (e.g. a malformed pattern)
and an execution error, so opting in means any PCRE failure becomes a
single catchable exception.
Also, preg_replace() and preg_filter() gain an optional $flags
parameter to accept it (the only two without one as far as I know).
Concretely, the check you'd write today:
if (preg_match($pattern, $subject, $m) === false) {
throw new RuntimeException(preg_last_error_msg());
}
...collapses to:
preg_match($pattern, $subject, $m, PREG_THROW_ON_ERROR);
First, I'd like to know whether there's interest at all and whether anyone
sees a reason not to add it.
If there is interest, a couple of design questions I'd want the list's read
on:
- Array subjcets. Currently
preg_last_error()reflects only the last
element processed, but I'd have the flag throw on the first failing
element rather than keep last-one-wins...does that seem right? - Whether
*_ON_ERRORreads better than*_ON_FAILUREgiven the
existingpreg_last_error()/PREG_*_ERRORvocabulary.
If it seems worth pursuing, I'll write it up as a proper RFC.
Thanks,
Osama