Skip to content

Instantly share code, notes, and snippets.

@smichaelsen
Last active November 2, 2021 13:12
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save smichaelsen/717fae9055ae83ed8e15 to your computer and use it in GitHub Desktop.
Save smichaelsen/717fae9055ae83ed8e15 to your computer and use it in GitHub Desktop.
PHP: Check if string is valid regular expression
<?php
function isRegularExpression($string) {
set_error_handler(function() {}, E_WARNING);
$isRegularExpression = preg_match($string, "") !== FALSE;
restore_error_handler();
return isRegularExpression;
}
@Quacky2200
Copy link

This could actually be a one-liner if you don't mind using @ symbols 👍

<?php
function isRegularExpression($string) {
    return @preg_match($string, '') !== FALSE;
}
?>

@dregad
Copy link

dregad commented Sep 11, 2020

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