0

I'm trying to validate that a string contains a regular expression, and that it is a valid one in PHP. Usually, I'd do this in the following way

<?php
@preg_match($string, '') !== false;

That generates a warning, which is fine because we use @ to suppress it. However, problems arise when we use set_error_handler to catch errors, as the handler will still be triggered, despite the @ supressor.

I'd like to do something similar to the code provided, without it throwing a warning.

The warning thrown is:

preg_match(): Delimiter must not be alphanumeric or backslash
Nate Higgins
  • 2,104
  • 16
  • 21

1 Answers1

3

Just add this in your error handler:

function user_error_handler($severity, $msg, $filename, $linenum, $content) {
    if (0 == (error_reporting() & $severity)) return;
    ...
}

In this case, because of the @ operator, error_reporting() will return 0.

linepogl
  • 9,147
  • 4
  • 34
  • 45