diff --git a/NEWS b/NEWS index 836ca453fef..4e2ecddcd85 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,9 @@ PHP NEWS . Introduced Zend guard recursion protection to fix __debugInfo issue. (Jakub Zelenka) +- Standard: + . Added $before_needle argument to strrchr(). (HypeMC) + 17 Aug 2023, PHP 8.3.0beta3 - Core: diff --git a/UPGRADING b/UPGRADING index 545b248c2bf..1e7f2069dda 100644 --- a/UPGRADING +++ b/UPGRADING @@ -348,6 +348,8 @@ PHP 8.3 UPGRADE NOTES means that when $decimals is negative, $num is rounded to $decimals significant digits before the decimal point. Previously negative $decimals got silently ignored and the number got rounded to zero decimal places. + . The $before_needle argument added to strrchr() which works in the same way + like its counterpart in strstr() or stristr(). ======================================== 6. New Functions diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index 6fed9c8064a..66b458897ad 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -2395,7 +2395,7 @@ function strripos(string $haystack, string $needle, int $offset = 0): int|false * @compile-time-eval * @refcount 1 */ -function strrchr(string $haystack, string $needle): string|false {} +function strrchr(string $haystack, string $needle, bool $before_needle = false): string|false {} /** @compile-time-eval */ function str_contains(string $haystack, string $needle): bool {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index f325634e95c..36b9cb1b9dc 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: decfa1e3d862d81880ea18150e2ba239bf15b8af */ + * Stub hash: 487cee0751d47b18bf0a8fbdb050313783f1b369 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -913,10 +913,7 @@ ZEND_END_ARG_INFO() #define arginfo_strripos arginfo_strpos -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strrchr, 0, 2, MAY_BE_STRING|MAY_BE_FALSE) - ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, needle, IS_STRING, 0) -ZEND_END_ARG_INFO() +#define arginfo_strrchr arginfo_stristr ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_str_contains, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, haystack, IS_STRING, 0) diff --git a/ext/standard/string.c b/ext/standard/string.c index c6b89897ef8..7efca1825b1 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -1656,7 +1656,7 @@ PHP_FUNCTION(stristr) if (part) { RETURN_STRINGL(ZSTR_VAL(haystack), found_offset); } - RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset); + RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset); } /* }}} */ @@ -1684,7 +1684,7 @@ PHP_FUNCTION(strstr) if (part) { RETURN_STRINGL(ZSTR_VAL(haystack), found_offset); } - RETURN_STRINGL(ZSTR_VAL(haystack) + found_offset, ZSTR_LEN(haystack) - found_offset); + RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset); } /* }}} */ @@ -1934,10 +1934,13 @@ PHP_FUNCTION(strrchr) zend_string *haystack, *needle; const char *found = NULL; zend_long found_offset; + bool part = 0; - ZEND_PARSE_PARAMETERS_START(2, 2) + ZEND_PARSE_PARAMETERS_START(2, 3) Z_PARAM_STR(haystack) Z_PARAM_STR(needle) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(part) ZEND_PARSE_PARAMETERS_END(); found = zend_memrchr(ZSTR_VAL(haystack), *ZSTR_VAL(needle), ZSTR_LEN(haystack)); @@ -1945,6 +1948,9 @@ PHP_FUNCTION(strrchr) RETURN_FALSE; } found_offset = found - ZSTR_VAL(haystack); + if (part) { + RETURN_STRINGL(ZSTR_VAL(haystack), found_offset); + } RETURN_STRINGL(found, ZSTR_LEN(haystack) - found_offset); } /* }}} */ diff --git a/ext/standard/tests/strings/strrchr_basic.phpt b/ext/standard/tests/strings/strrchr_basic.phpt index 23ae46fd144..870742a9ab8 100644 --- a/ext/standard/tests/strings/strrchr_basic.phpt +++ b/ext/standard/tests/strings/strrchr_basic.phpt @@ -4,49 +4,79 @@ Test strrchr() function : basic functionality --EXPECT-- *** Testing strrchr() function: basic functionality *** string(12) "Hello, World" +string(0) "" string(12) "Hello, World" +string(0) "" string(12) "Hello, World" +string(0) "" string(12) "Hello, World" +string(0) "" +bool(false) +bool(false) bool(false) bool(false) string(5) "World" +string(7) "Hello, " string(5) "World" +string(7) "Hello, " string(7) ", World" +string(5) "Hello" string(7) ", World" +string(5) "Hello" string(12) "Hello, World" +string(0) "" string(12) "Hello, World" +string(0) "" string(4) "orld" +string(8) "Hello, W" string(4) "orld" +string(8) "Hello, W" +bool(false) bool(false) *** Done ***