Menu Home

preg_last_error

osbo.com | PHP | Functions | preg_last_error

Returns the error code of the last PCRE regex execution

Syntax

preg_last_error ( void ) : int

Return

Returns one of the following constants:

PREG_NO_ERROR

PREG_INTERNAL_ERROR

PREG_BACKTRACK_LIMIT_ERROR (see also pcre.backtrack_limit)

PREG_RECURSION_LIMIT_ERROR (see also pcre.recursion_limit)

PREG_BAD_UTF8_ERROR

PREG_BAD_UTF8_OFFSET_ERROR

PREG_JIT_STACKLIMIT_ERROR

Examples

1

<?

$pattern = '#(?:\D+|<\d+>)*[!?]#';
$subject = 'testing testing testing';

preg_match($pattern, $subject);

$return = preg_last_error();

echo $return;

?>
2

2

<?

$pattern = '#(?:\D+|<\d+>)*[!?]#';
$subject = 'testing testing testing';

preg_match($pattern, $subject);

$return = preg_last_error();

switch ($return)
{
    case PREG_NO_ERROR:
        echo "PREG_NO_ERROR";
        break;
    case PREG_INTERNAL_ERROR:
        echo "PREG_INTERNAL_ERROR";
        break;
    case PREG_BACKTRACK_LIMIT_ERROR:
        echo "PREG_BACKTRACK_LIMIT_ERROR";
        break;
    case PREG_RECURSION_LIMIT_ERROR:
        echo "PREG_RECURSION_LIMIT_ERROR";
        break;
    case PREG_BAD_UTF8_ERROR:
        echo "PREG_BAD_UTF8_ERROR";
        break;
    case PREG_BAD_UTF8_OFFSET_ERROR:
        echo "PREG_BAD_UTF8_OFFSET_ERROR";
        break;
    case PREG_JIT_STACKLIMIT_ERROR:
        echo "PREG_JIT_STACKLIMIT_ERROR";
        break;
}

?>
PREG_BACKTRACK_LIMIT_ERROR

3

<?

$pattern = '#(?:\D+|<\d+>)*[!?]#';
$subject = 'testing testing testing';

preg_match($pattern, $subject);

$return = preg_last_error();

$pcre = get_defined_constants(true)["pcre"];
$pcre_error = array();

foreach ($pcre as $key => $value)
{
    if (preg_match('#ERROR#', $key))
    {
        $pcre_error[$value] = $key;
    }
}

echo $pcre_error[$return];

?>
PREG_BACKTRACK_LIMIT_ERROR

PHP