Skip to content

Commit 4407588

Browse files
SpacePossumnicolas-grekas
authored andcommitted
[Php80] Fix str_ends_with() when needle is longer than haystack
1 parent 752f73d commit 4407588

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Php80.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ public static function str_starts_with(string $haystack, string $needle): bool
100100

101101
public static function str_ends_with(string $haystack, string $needle): bool
102102
{
103-
return '' === $needle || ('' !== $haystack && 0 === substr_compare($haystack, $needle, -\strlen($needle)));
103+
if ('' === $needle || $needle === $haystack) {
104+
return true;
105+
}
106+
107+
if ('' === $haystack) {
108+
return false;
109+
}
110+
111+
$needleLength = \strlen($needle);
112+
113+
return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength);
104114
}
105115
}

0 commit comments

Comments
 (0)