Drupal has its own custom error handler, so when I try to get the message from the last warning (e.g. for an invalid regex), there is nothing there:

if (@preg_match($regex, NULL) === FALSE) {

The @ prevents it from being displayed on the screen, and without that I can see a warning for a $regex of '/(/' (about an unmatched paren), but the warning is not shown by dpm(), but rather by Drupal.

Is there a way I can get the warning from an invalid regex in this example, so that I could have the message in a variable?


Use the same PHP's error functions as Drupal to toggle PHP's error handling behavior.

In particular, you can use set_error_handler() / restore_error_handler() to toggle the PHP error code handling while you're debugging:

// Remove _drupal_error_handler() handling

if (@preg_match($regex, NULL) === FALSE) {
// Restore _drupal_error_handler() handling

See the PHP doc links for more information on how these error handling functions work.

  • 1
    This does technically answer my question, but it ended up not working, because I found out that the preg_match warning is thrown by the Drupal error handler. So restoring the error handler removed the warning completely. – mbomb007 Apr 2 at 14:02
  • I ended up using this answer – mbomb007 Apr 2 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.