Skip to content

Commit bd7e4b8

Browse files
Nicolas Oelgartnicolas-grekas
authored andcommitted
[PHP 8.0] Add preg_last_error_msg function
1 parent b0bb138 commit bd7e4b8

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Php80.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
* @author Ion Bazan <[email protected]>
16+
* @author Nico Oelgart <[email protected]>
1617
*
1718
* @internal
1819
*/
@@ -26,6 +27,28 @@ public static function fdiv($dividend, $divisor)
2627
return (float) @($dividend / $divisor);
2728
}
2829

30+
public static function pregLastErrorMsg()
31+
{
32+
switch (preg_last_error()) {
33+
case PREG_INTERNAL_ERROR:
34+
return 'Internal error';
35+
case PREG_BAD_UTF8_ERROR:
36+
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
37+
case PREG_BAD_UTF8_OFFSET_ERROR:
38+
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
39+
case PREG_BACKTRACK_LIMIT_ERROR:
40+
return 'Backtrack limit exhausted';
41+
case PREG_RECURSION_LIMIT_ERROR:
42+
return 'Recursion limit exhausted';
43+
case PREG_JIT_STACKLIMIT_ERROR:
44+
return 'JIT stack limit exhausted';
45+
case PREG_NO_ERROR:
46+
return 'No error';
47+
default:
48+
return 'Unknown error';
49+
}
50+
}
51+
2952
private static function floatArg($value, $caller, $pos)
3053
{
3154
if (\is_float($value)) {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This component provides functions added to PHP 8.0 core:
66
- [`fdiv`](https://php.net/fdiv) (only for PHP 7.0+)
77
- `ValueError` class
88
- `FILTER_VALIDATE_BOOL` constant
9+
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
910

1011
More information can be found in the
1112
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
function fdiv($dividend, $divisor) { return p\Php80::fdiv($dividend, $divisor); }
1717
}
1818

19+
if (!function_exists('preg_last_error_msg')) {
20+
function preg_last_error_msg() { return p\Php80::pregLastErrorMsg(); }
21+
}
22+
1923
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2024
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2125
}

0 commit comments

Comments
 (0)