The preg_last_error_msg Function

The preg_last_error_msg function returns a human-readable message about the last error that occurred when working with regular expression functions. This is a convenient alternative to the preg_last_error function, which returns only an error code.

Syntax

phppreg_last_error_msg(): string

Example

Let's try to execute an invalid regular expression and get an error message:

php<?php
  preg_match('/invalid(regex/', 'test string');
  echo preg_last_error_msg();
?>

Code execution result:

php'No ending matching delimiter found'

Example

Let's check the error message when the backtrack limit is exceeded:

php<?php
  ini_set('pcre.backtrack_limit', 1);
  preg_match('/(\d+)+$/', str_repeat('1', 1000));
  echo preg_last_error_msg();
?>

Code execution result:

php'Backtrack limit exhausted'

See Also

  • the preg_last_error function,
    which returns the code of the last PCRE error
  • the preg_match function,
    which performs a regular expression match