Last active
January 25, 2025 05:35
-
-
Save smichaelsen/717fae9055ae83ed8e15 to your computer and use it in GitHub Desktop.
PHP: Check if string is valid regular expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isRegularExpression($string) { | |
set_error_handler(function() {}, E_WARNING); | |
$isRegularExpression = preg_match($string, "") !== FALSE; | |
restore_error_handler(); | |
return isRegularExpression; | |
} |
This could actually be a one-liner if you don't mind using @ symbols +1
Except that @
can sometimes be disabled (with xdebug or scream extension).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could actually be a one-liner if you don't mind using
@
symbols 👍