diff --git a/src/Utils/Strings.php b/src/Utils/Strings.php index 77553a56f..958823d1e 100644 --- a/src/Utils/Strings.php +++ b/src/Utils/Strings.php @@ -456,6 +456,8 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in $len = strlen($haystack); if ($needle === '') { return $len; + } elseif ($len === 0) { + return null; } $pos = $len - 1; while (($pos = strrpos($haystack, $needle, $pos - $len)) !== false && ++$nth) { diff --git a/tests/Utils/Strings.indexOf().phpt b/tests/Utils/Strings.indexOf().phpt index b89113813..5a89e7bc6 100644 --- a/tests/Utils/Strings.indexOf().phpt +++ b/tests/Utils/Strings.indexOf().phpt @@ -31,6 +31,8 @@ test('', function () { Assert::null(Strings::indexOf($foo, 'not-in-string')); Assert::null(Strings::indexOf($foo, 'b', -2)); Assert::null(Strings::indexOf($foo, 'b', 2)); + Assert::null(Strings::indexOf('', 'a', 1)); + Assert::null(Strings::indexOf('', 'a', -1)); });