Skip to content

Core\Str::endsWith

Checks whether a string ends with a piece of text.

Core\Str::endsWith returns true when the last characters of the string are exactly the text you give it, and false otherwise. A text longer than the string is never at its end, so the result is false.

The check is case-sensitive, so "photo.JPG" does not end with ".jpg". To ignore upper and lower case, change the string to lower case with Core\Str::lower first. An empty text is at the end of every string, so the result is true.

The text must also start at a whole character, the way a person reads it. "\r\n" is one line ending, and it counts as one character. So Core\Str::endsWith("line\r\n", "\n") is false. Check for "\r\n", or remove the line break with Core\Str::trimEnd first.

Core\Str::beforeSuffix does the check and returns the text before the suffix in one call. It returns null when the string does not end with the text. Core\Str::startsWith does the same check at the start of a string.

The examples below check a few endings, add a missing / to the end of a web address, and accept only uploads that are images.

Parameters

  • $sstringThe string to check.
  • $suffixstringThe text $s must end with. The check is case-sensitive. $suffix must start at a whole character: "\n" does not match the end of "a\r\n".

Return value

Returns bool. true when $s ends with $suffix, otherwise false. An empty $suffix is at the end of every string, so the result is true.

Changelog

VersionChange
0.0.6Core\Str::startsWith, Core\Str::endsWith and Core\Str::replaceAll find a match only where it does not split a character. So Core\Str::startsWith("e\u{301}", "e") is false, because the accent belongs to the e. The pair "\r\n" is one character, so "\r" alone does not match inside it.

Examples

Check an ending

<?nvs
// Shows how to check whether a string ends with a piece of text.
string $file = 'report-2024.pdf';
// The string ends with ".pdf". This prints `true`.
echo Core\Str::endsWith($file, '.pdf') ? 'true' : 'false', "\n";
// "2024" is in the string, but not at its end. This prints `false`.
echo Core\Str::endsWith($file, '2024') ? 'true' : 'false', "\n";
// The check is case-sensitive. This prints `false`.
echo Core\Str::endsWith($file, '.PDF') ? 'true' : 'false', "\n";
// An empty text is at the end of every string. This prints `true`.
echo Core\Str::endsWith($file, '') ? 'true' : 'false', "\n";
Output
true
false
false
true

Add a missing slash

<?nvs
// Shows how to make sure every web address ends with one `/`.
array<string> $addresses = ['https://example.com/shop', 'https://example.com/blog/'];
foreach ($addresses as string $address) {
// Only the address without a `/` at the end gets one added.
if (!Core\Str::endsWith($address, '/')) {
$address = $address . '/';
}
// Both lines end with exactly one `/`.
echo $address, "\n";
}
Output
https://example.com/shop/
https://example.com/blog/

Accept only images

<?nvs
// Shows how to accept only uploaded files whose names end like an image.
class Upload
{
public static function isImage(string $name): bool
{
// The name is changed to lower case first, so ".PNG" also counts.
string $lower = Core\Str::lower($name);
return Core\Str::endsWith($lower, '.png')
|| Core\Str::endsWith($lower, '.jpg')
|| Core\Str::endsWith($lower, '.jpeg');
}
}
array<string> $uploads = ['holiday.JPG', 'logo.png', 'invoice.pdf', 'photo.png.exe'];
// This prints `accepted` for the first two files and `rejected` for the other two.
foreach ($uploads as string $name) {
echo $name, ': ', Upload::isImage($name) ? 'accepted' : 'rejected', "\n";
}
Output
holiday.JPG: accepted
logo.png: accepted
invoice.pdf: rejected
photo.png.exe: rejected