209 of 410 menu

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

preg_last_error_msg(): string

Example

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

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

Code execution result:

'No ending matching delimiter found'

Example

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

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

Code execution result:

'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
byenru