In this article, you will learn about preg_last_error() method in PHP. The preg_last_error() function in PHP returns an error code for the most recently evaluated regular expression. The returned value will match one of the following constants:
what is the syntax of the PREG_LAST_ERROR function in php?
preg_last_error()
examples of the PREG_LAST_ERROR function
Example 1. In this example, we use preg_last_error() to handle errors.
<?php
$str = 'The regular expression is invalid.';
$pattern = '/invalid//';
$match = @preg_match($pattern, $str, $matches);
if($match === false) {
// An error occurred
$err = preg_last_error();
if($err == PREG_INTERNAL_ERROR) {
echo 'Invalid regular expression.';
}
} else if($match) {
// A match was found
echo $matches[0];
} else {
// No matches were found
echo 'No matches found';
}
?>