Core\Str::beforeSuffix
Core\Str::beforeSuffix(string $s, string $suffix): ?stringCore\Str::beforeSuffix( string $s, string $suffix, ): ?string
Returns the start of a string before a given end, or null if the string does not end with it.
Core\Str::beforeSuffix checks whether the string ends with the suffix. If it does, the result is everything before the suffix. If it does not, the result is null. So one call does the check and the cut. If the suffix is the whole string, the result is an empty string. An empty suffix gives the whole string back.
The check is case-sensitive: ".CSV" does not match ".csv". The suffix must also start at a whole character, the way a person reads it. For example, "\r\n" is one line ending, and it counts as one character. The suffix "\n" starts inside that character, so the result is null. Core\Str::endsWith uses the same rule, so it returns false for these two strings. Core\Str::afterPrefix does the same at the start of a string.
The examples below take the name of a file without its extension, remove a trailing / from a path, and show that a suffix cannot start inside a character.
Parameters
$sstringThe string to cut.$suffixstringThe text to remove from the end of$s. The check is case-sensitive.$suffixmust start at a whole character:"\u{301}"does not match the end of"e\u{301}".
Return value
Returns ?string. The part of $s before $suffix. If $s does not end with $suffix, the result is null. An empty $suffix gives $s, and a $suffix equal to $s gives an empty string.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Str::afterPrefix checks that a string starts with a prefix and returns the rest of the string. Core\Str::beforeSuffix does the same at the end. Both return null when the string does not match. |
Examples
File extension
<?nvs// Shows how to take a file name without its ".csv" extension.
array<string> $files = ['orders.csv', 'notes.txt', 'backup.csv.tmp', 'PRICES.CSV'];
foreach ($files as string $file) { // "orders.csv" ends with ".csv", so this prints `orders`. // The other names do not end with ".csv", so the result is `null`. ?string $name = Core\Str::beforeSuffix($file, '.csv'); echo $file, ': ', $name ?? '(not a CSV file)', "\n";}orders.csv: ordersnotes.txt: (not a CSV file)backup.csv.tmp: (not a CSV file)PRICES.CSV: (not a CSV file)Trailing slash
<?nvs// Shows how to remove one trailing "/" from a path, so "/shop/" and "/shop" are the same.
array<string> $paths = ['/shop/', '/shop', '/'];
foreach ($paths as string $path) { // A path without a trailing "/" gives `null`, and `??` keeps the path as it is. // The path "/" gives an empty string. This prints `[/shop]`, `[/shop]` and `[]`. string $clean = Core\Str::beforeSuffix($path, '/') ?? $path; echo '[', $clean, "]\n";}[/shop][/shop][]Whole characters
<?nvs// Shows that a suffix matches only whole characters.
// "\r\n" is one character, so a suffix of "\n" alone starts inside it.// The result is `null`, and this prints `(none)`.echo Core\Str::beforeSuffix("total: 3\r\n", "\n") ?? '(none)', "\n";
// The suffix "\r\n" is the whole character, so this prints `total: 3`.echo Core\Str::beforeSuffix("total: 3\r\n", "\r\n") ?? '(none)', "\n";
// "e" followed by a combining accent (U+0301) is one character: "é".// The accent alone is not a whole character, so this prints `(none)`.echo Core\Str::beforeSuffix("cafe\u{301}", "\u{301}") ?? '(none)', "\n";
// `Core\Str::endsWith` gives the same answer for the same two strings.// This prints `false`.echo Core\Str::endsWith("cafe\u{301}", "\u{301}") ? 'true' : 'false', "\n";(none)total: 3(none)false