Skip to content

Core\Str::trimEnd

Core\Str::trimEnd(string $s, {characters?: string}): stringCore\Str::trimEnd(
    string $s,
    {characters?: string},
): string

Removes characters from the end of a string.

Core\Str::trimEnd returns the string without the spaces, tabs and line breaks at its end. The start is not changed, so an indented line keeps its indent.

The characters option gives your own list of characters to remove. It replaces the default list, so { characters: "/" } removes only /. Each character in the list counts on its own, and the order does not matter. If every character is in the list, the result is an empty string.

A trim removes whole characters, as a person counts them. Some characters have several code points (Unicode numbers). Such a character is removed only when all its code points are in the list. So Core\Str::trimEnd("e\u{301}", { characters: "\u{301}" }) returns "e\u{301}" unchanged. The default list removes the "\r\n" at the end of a line read from a file, because both its code points are in it.

Core\Str::trim removes characters from both ends, and Core\Str::trimStart from the start only.

The examples below remove the line break at the end of a line, remove the slashes at the end of a web address, and show a price without the zeros it does not need.

Parameters

  • $sstringThe string to trim.

Options

  • charactersstringoptionalThe characters to remove, in any order. a..z is the three characters a, . and z. The default is space, tab, newline, carriage return, NUL and vertical tab. A character made of several code points, such as e with an accent mark, is removed only when all of its code points are in this set.

Return value

Returns string. $s without those characters at its end. The rest of $s is not changed.

Changelog

VersionChange
0.0.6Core\Str::trim, Core\Str::trimStart and Core\Str::trimEnd remove whole characters. A character at the end is removed only when the characters option has every code point of it. So Core\Str::trim("e\u{301}", { characters: "\u{301}" }) returns the text unchanged, because the accent belongs to the e.

Examples

Remove a line break

<?nvs
// Shows how to remove the line break and spaces at the end of a line, and keep its indent.
string $line = " total: 12 \r\n";
// Only the end changes. This prints `[ total: 12]`.
echo '[', Core\Str::trimEnd($line), "]\n";
// A string of spaces only becomes the empty string. This prints `[]`.
echo '[', Core\Str::trimEnd(" \n "), "]\n";
Output
[ total: 12]
[]

Remove trailing slashes

<?nvs
// Shows how to remove every `/` at the end of a web address before a path is added to it.
array<string> $bases = ['https://example.com', 'https://example.com/', 'https://example.com///'];
foreach ($bases as string $base) {
// All three lines print `https://example.com/api/orders`.
echo Core\Str::trimEnd($base, { characters: '/' }), "/api/orders\n";
}
Output
https://example.com/api/orders
https://example.com/api/orders
https://example.com/api/orders

Show a short price

<?nvs
// Shows how to print a price without the zeros it does not need.
class Price
{
public static function short(string $amount): string
{
// Zeros at the end are removed first. Then a `.` with nothing after it is removed.
return Core\Str::trimEnd(Core\Str::trimEnd($amount, { characters: '0' }), { characters: '.' });
}
}
array<string> $amounts = ['12.50', '3.00', '100.25'];
// This prints `12.5`, `3` and `100.25`.
foreach ($amounts as string $amount) {
echo Price::short($amount), "\n";
}
Output
12.5
3
100.25