<?php /** * Return an error message if the given pattern argument or its underlying regular expression * are not syntactically valid. Otherwise (if they are valid), NULL is returned. * * @param $pattern * * @return string|null */ function regexHasErrors($pattern): ?string { if(@preg_match($pattern, '') === false){ return str_replace("preg_match(): ", "", error_get_last()["message"]); //Make it prettier by removing the function name prefix } return NULL; } $pattern = "/incorrect)/"; if($error = regexHasErrors($pattern)){ echo $error; }
You have javascript disabled. You will not be able to edit any code.