Re: [DISCUSSION] Validating regex pattern
| From: | Alexandre Daubois | Date: | Thu, 02 Oct 2025 08:30:37 +0000 |
| Subject: | Re: [DISCUSSION] Validating regex pattern | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128767@lists.php.net to get a copy of this message | ||
Hi,
> function is_valid_preg_pattern( $pattern ) {
> $is_valid = true;
>
> set_error_handler( function () use ( &$is_valid ) { $is_valid = false; return true;
> }, E_WARNING );
> preg_match( $pattern, '' );
> restore_error_handler();
>
> return $is_valid;
> }
Setting and restoring the global error handler to validate a pattern
is exactly what I would qualify as clunky/hacky. Also this requires
internal knowledge on how this works, which feels very wrong for
something like that.
— Alexandre Daubois