private function validateRegularExpression($regex)
    {
        $error_string = null;

        // Set an error handler temporarily
        set_error_handler(
            function ($errno, $errstr) use (&$error_string) {
                $error_string = $errstr;
            },
            E_NOTICE | E_WARNING
        );

        if (false === preg_match($regex, null) || !empty($error_string)) {
            // Remove our temporary error handler
            restore_error_handler();

            throw new RegularExpressionCompilationException(
                $error_string,
                preg_last_error()
            );
        }

        // Remove our temporary error handler
        restore_error_handler();

        return true;
    }
private function validateRegularExpression($regex)
    {
        $error_string = null;

        // Set an error handler temporarily
        set_error_handler(
            function ($errno, $errstr) use (&$error_string) {
                $error_string = $errstr;
            },
            E_NOTICE | E_WARNING
        );

        if (false === preg_match($regex, null) || !empty($error_string)) {
            // Remove our temporary error handler
            restore_error_handler();

            throw new RegularExpressionCompilationException(
                $error_string,
                preg_last_error()
            );
        }

        // Remove our temporary error handler
        restore_error_handler();

        return true;
    }
private function validateRegularExpression ( $ regex ) { $ error_string = null ; // Set an error handler temporarily set_error_handler ( function ( $ errno , $ errstr ) use ( & $ error_string ) { $ error_string = $ errstr ; } , E_NOTICE | E_WARNING ) ; if ( false === preg_match ( $ regex , null ) || ! empty ( $ error_string ) ) { // Remove our temporary error handler restore_error_handler ( ) ; throw new RegularExpressionCompilationException ( $ error_string , preg_last_error ( ) ) ; } // Remove our temporary error handler restore_error_handler ( ) ; return true ; }
Validate a regular expression

This simply checks if the regular expression is able to be compiled
and converts any warnings or notices in the compilation to an exception

@param string $regex                          The regular expression to validate
@throws RegularExpressionCompilationException If the expression can't be compiled
@return boolean
Validate a regular expression