Categories
PHP

Handling Errors in Regular Expressions

The regular expression functions in PHP do not throw exceptions, so if there is an error in the regular expression, what will you do? To handle regular expressions errors, PHP offers two functions : preg_last_error() and preg_last_error_msg().

preg_last_error

<?php
 //Syntax
 preg_last_error(): int

The preg_last_error() function does not take any parameter, it returns one of the following constants:

  • PREG_NO_ERROR
    Returned if there were no errors.
  • PREG_INTERNAL_ERROR
    Returned if there was an internal regex error.
  • PREG_BACKTRACK_LIMIT_ERROR
    Returned if backtrack limit was exhausted, the default limit is 1000000.
  • PREG_RECURSION_LIMIT_ERROR
    Returned if recursion limit was exhausted, the default limit is 100000.
  • PREG_BAD_UTF8_ERROR
    Returned if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode).
  • PREG_BAD_UTF8_OFFSET_ERROR
    Returned if the offset didn’t correspond to the begin of a valid UTF-8 code point (only when running a regex in UTF-8 mode).
  • PREG_JIT_STACKLIMIT_ERROR
    Returned if the function failed due to limited JIT (just-in-time compilation) stack space, the default limit is 1.

Example: Check whether the regular expression is successful

<?php
 $backSlash = 'The backsash \ character';
 $pattern = '/^[a-zA-Z \\\\]*$/';
 $found = preg_match($pattern, $backSlash, $match);
 
 if (preg_last_error() === PREG_NO_ERROR) {
  //Proceed, no errors
 }

Example: Determine the error type in a regular expression

If you’re using PHP8 or above, use preg_last_error_msg() function instead.

<?php
 $backSlash = 'The backsash \ character';
 $pattern = '/^[a-zA-Z \\\\]*$/';
 $found = preg_match($pattern, $backSlash, $match);
 
 if (preg_last_error() !== PREG_NO_ERROR) {
  switch(preg_last_error()){
   case PREG_INTERNAL_ERROR:
    echo 'Internal error';
    break;
   case PREG_BACKTRACK_LIMIT_ERROR:
    echo 'Backtrack limit error';
    break;
   case PREG_RECURSION_LIMIT_ERROR:
    echo 'Recursion limit error';
    break;
   case PREG_BAD_UTF8_ERROR:
    echo 'Bad UTF8 error';
    break;
   case PREG_BAD_UTF8_OFFSET_ERROR:
    echo 'Bad UTF8 offset error';
    break;
   case PREG_JIT_STACKLIMIT_ERROR:
   echo 'JIT Compilation Stack Limit Error';
    break;   
  }
 }

preg_last_error_msg

<?php
 //Syntax
 preg_last_error_msg(): string

The preg_last _error_msg() function was introduced in PHP 8, in previous visions of PHP users only rely on preg_last_error(), which returns error codes. You can use this function if you want friendly error messages instead of just error codes.

<?php
 $backSlash = 'The backsash \ character';
 $pattern = '/^[a-zA-Z \\\\]*$/';
 $found = preg_match($pattern, $backSlash, $match);

 if (preg_last_error() !== PREG_NO_ERROR) {
  echo preg_last_error_msg();
 }

More Regular Expressions Tutorials:

  1. Regular Expressions
  2. Matching patterns using preg_match() and preg_match_all()
  3. Search and replace with preg_replace() and preg_filter()
  4. Search and replace with preg_replace_callback() and preg_replace_callback_array()
  5. Regular expression to find in an array and to split a string
  6. Escaping special characters in regular expressions
  7. Handling errors in regular expressions
  8. Matching word boundaries
  9. Data validation with regular expressions