It is best to just pass the preg_match string and catch any errors that happen.
try{ preg_match($in_regex, $string, $results); //Use $results } catch (Exception $e) { echo "Sorry, bad regex (/" . $in_regex . "/)"; }
[Edit] Since this does not work, you can try:
function bad_regex($errno, $errstr, $errfile, $errline){ echo "Sorry, bad regex."; } set_error_handler("bad_regex"); preg_match($in_regex, $string, $results); restore_error_handler();
source share