Core\Str::trim
Core\Str::trim(string $s, {characters?: string}): stringCore\Str::trim( string $s, {characters?: string}, ): string
Removes characters from both ends of a string.
Core\Str::trim returns the string without the spaces, tabs and line breaks at its start and its end. The characters in the middle of the string are not changed. Text that people type, or that comes from a file, often has extra spaces like these.
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. "a..z" is the three characters a, . and z. It is not a range of letters.
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::trim("e\u{301}", { characters: "\u{301}" }) returns "e\u{301}" unchanged. The default list removes "\r\n", because both its code points are in it.
Core\Str::trimStart removes characters from the start only, and Core\Str::trimEnd from the end only.
The examples below clean up text that somebody typed, remove quotes and dashes, and read the settings from a configuration file.
Parameters
$sstringThe string to trim.
Options
charactersstringoptionalThe characters to remove, in any order.a..zis the three charactersa,.andz. The default is space, tab, newline, carriage return, NUL and vertical tab. A character made of several code points, such asewith 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 start and its end. The middle of $s is not changed.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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
Clean typed text
<?nvs// Shows how to remove the spaces and line breaks that people type by mistake.
string $typed = " anna@example.com \n";
// The spaces and the line break at both ends are removed.// This prints `[anna@example.com]`.echo '[', Core\Str::trim($typed), "]\n";
// The space in the middle stays. This prints `[New York]`.echo '[', Core\Str::trim("\t New York "), "]\n";
// A string of spaces only becomes the empty string. This prints `[]`.echo '[', Core\Str::trim(' '), "]\n";[anna@example.com][New York][]Remove your own characters
<?nvs// Shows how to choose which characters `Core\Str::trim` removes.
// Only the quotes are removed. This prints `[hello]`.echo '[', Core\Str::trim("\"hello\"", { characters: "\"" }), "]\n";
// Each character in the list counts on its own, so `-` and `/` are both removed.// This prints `[docs/intro]`.echo '[', Core\Str::trim('--/docs/intro/--', { characters: '-/' }), "]\n";
// Your list replaces the default one, so the spaces stay. This prints `[ x ]`.echo '[', Core\Str::trim('- x -', { characters: '-' }), "]\n";[hello][docs/intro][ x ]Read a settings file
<?nvs// Shows how to read `name = value` lines from a settings file, with spaces around each part.
string $file = " title = My Shop \nlanguage=en\n \n currency = EUR\n";
foreach (Core\Str::split($file, "\n") as string $line) { // A line with nothing but spaces is skipped. if (Core\Str::trim($line) == '') { continue; } // The line is cut at the first `=`, and both parts are trimmed. array<string> $parts = Core\Str::split($line, '=', { limit: 2 }); // This prints `title: My Shop`, `language: en` and `currency: EUR`. echo Core\Str::trim($parts[0]), ': ', Core\Str::trim($parts[1]), "\n";}title: My Shoplanguage: encurrency: EUR