In PHP 5.5 and above, getting the error message is as simple as:
<?php
echo array_flip(get_defined_constants(true)['pcre'])[preg_last_error()];
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
preg_last_error — Retourne le code erreur de la dernière expression PCRE exécutée
Retourne le code erreur de la dernière regex PCRE exécutée.
Exemple #1 Exemple avec preg_last_error()
<?php
preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
echo 'Backtrack limit was exhausted!';
}
?>
L'exemple ci-dessus va afficher :
Backtrack limit was exhausted!
Cette fonction ne contient aucun paramètre.
Retourne une des constantes suivantes (expliquées sur cette page):
PREG_NO_ERROR
PREG_INTERNAL_ERROR
PREG_BACKTRACK_LIMIT_ERROR
(voir aussi pcre.backtrack_limit)PREG_RECURSION_LIMIT_ERROR
(voir aussi pcre.recursion_limit)PREG_BAD_UTF8_ERROR
PREG_BAD_UTF8_OFFSET_ERROR
PREG_JIT_STACKLIMIT_ERROR
In PHP 5.5 and above, getting the error message is as simple as:
<?php
echo array_flip(get_defined_constants(true)['pcre'])[preg_last_error()];
Here is a correction to one of the previous "get error message" snippets. It filters out the non-error codes which stops "Warning: array_flip(): Can only flip STRING and INTEGER values!" from being emitted due to ["PCRE_JIT_SUPPORT"]=>bool(true):
<?php
echo array_flip(array_filter(get_defined_constants(true)['pcre'], function ($value) {
return substr($value, -6) === '_ERROR';
}, ARRAY_FILTER_USE_KEY))[preg_last_error()];
Here is a more advanced function to convert an error code to text:
<?php
function preg_errtxt($errcode)
{
static $errtext;
if (!isset($errtxt))
{
$errtext = array();
$constants = get_defined_constants(true);
foreach ($constants['pcre'] as $c => $n) if (preg_match('/_ERROR$/', $c)) $errtext[$n] = $c;
}
return array_key_exists($errcode, $errtext)? $errtext[$errcode] : NULL;
}
?>
Take it into account before use this function.
My php --version
PHP 5.6.29 (cli) (built: Dec 8 2016 09:19:46)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
On Linux Fedora 23 4.8.13-100.fc23.x86_64 #1 SMP Fri Dec 9 14:51:40 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
<?php
// @see http://stackoverflow.com/questions/4440626/how-can-i-validate-regex
// returns true because there is no opening "(" for the closing ")"
var_dump(preg_match('~InvalidRegular)Expression~', null) === false);
// this SHOULD be something different from 0, but...
var_dump(preg_last_error());
?>
The above function pcre_error_deocde [sic] is not correct - not all of the used constants are errors constants. For example, when the error is actually PREG_BAD_UTF8_ERROR, the function outputs the text for PREG_SPLIT_OFFSET_CAPTURE.
Just an addition to my previous note: In unicode mode (with "u" modifier), PREG_BAD_UTF8_ERROR only reflects errors in the subject string. If the pattern itself contains invalid characters, preg_match() (or preg_match_all()) returns false but preg_last_error() returns 0 indicating PREG_NO_ERROR. Instead, php issues a warning: "preg_match(): Compilation failed: invalid UTF-8 string at offset 0"
PHP 8 has a native function to retrieve the actual error message, so these helper functions are no longer necessary.
https://www.php.net/preg_last_error_msg
<?php
preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
if (preg_last_error() !== PREG_NO_ERROR) {
echo preg_last_error_msg(); // Prints "Backtrack limit exhausted"
}
?>
In PHP 7.3+, and depending on the environment, there may be other constants that aren't ints which will cause errors with array_flip. I opted to go with a RegexException and a filter on gk's note.
<?php
class RegexException extends \Exception {
public $errorCode;
public function __construct(
int $errorCode,
string $additionalMessage = null
) {
$this->errorCode = $errorCode;
$errorMessage = $this->getErrorString($errorCode) ?? 'Unknown regex error';
$additionalMessage = $additionalMessage ? " $additionalMessage" : '';
parent::__construct("Regex error thrown: $errorMessage." . $additionalMessage);
}
/**
* Gets an error string (const name) for the PCRE error code
*
* @param int $errorCode
*
* @return string|null
*/
private function getErrorString(int $errorCode): ?string
{
$pcreConstants = get_defined_constants(true)['pcre'] ?? [];
/*
* NOTE: preg_last_error() returns an int, but there are constants
* in PHP 7.3+ that are strings, bools, or otherwise. We can pretty
* safely filter out the non-integers to fetch the appropriate
* error constant name.
*/
$pcreConstants = array_filter($pcreConstants, function ($code) {
return is_int($code);
});
$errorStrings = array_flip($pcreConstants);
return $errorStrings[$errorCode] ?? null;
}
Usage:
throw new RegexException(preg_last_error());
If you use T-Regx library, to get the last constant, you can use
<?php
echo preg_last_error_constant(); // 'PREG_BAD_UTF8_ERROR'
Getting error as text (small update):
<?php
echo array_flip(array_filter(get_defined_constants(true)['pcre'], function($v) { return is_integer($v); }))[preg_last_error()];