Skip to content

Instantly share code, notes, and snippets.

@ameenross
Forked from smichaelsen/check_regex.php
Last active April 29, 2021 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameenross/49d1789fccec34da82f8ba6e6e7de87a to your computer and use it in GitHub Desktop.
Save ameenross/49d1789fccec34da82f8ba6e6e7de87a to your computer and use it in GitHub Desktop.
PHP: Check if string is valid regular expression
<?php
/**
* @param string $regex
* @return bool
*/
function isValidRegularExpression($regex) {
set_error_handler(function() {}, E_WARNING);
$valid = preg_match($regex, null) !== FALSE;
restore_error_handler();
return $valid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment