Core\Str::afterPrefix
Core\Str::afterPrefix(string $s, string $prefix): ?stringCore\Str::afterPrefix( string $s, string $prefix, ): ?string
Returns the rest of a string after a given start, or null if the string does not start with it.
Core\Str::afterPrefix checks whether the string starts with the prefix. If it does, the result is everything after the prefix. If it does not, the result is null. So one call does the check and the cut. If the prefix is the whole string, the result is an empty string. An empty prefix gives the whole string back.
The check is case-sensitive: "Bearer " does not match "bearer ". The prefix must also end after a whole character, the way a person reads it. For example, "e\u{301}" is an "e" with a combining accent, and a person reads it as one character: "é". The prefix "e" ends inside that character, so the result is null. Core\Str::startsWith uses the same rule, so it returns false for these two strings. Core\Str::beforeSuffix does the same at the end of a string.
The examples below read a day number out of form field names, read the token out of an Authorization header, and show that a prefix cannot end inside a character.
Parameters
$sstringThe string to cut.$prefixstringThe text to remove from the start of$s. The check is case-sensitive.$prefixmust end after a whole character:"e"does not match the start of"e\u{301}".
Return value
Returns ?string. The rest of $s after $prefix. If $s does not start with $prefix, the result is null. An empty $prefix gives $s, and a $prefix 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
Form field names
<?nvs// Shows how to read a number out of form field names that share a start.
array<string> $keys = ['openingHours-12', 'name', 'openingHours-3', 'openingHoursNote'];
foreach ($keys as string $key) { // `Core\Str::afterPrefix` returns the text after "openingHours-". // A key that does not start with it gives `null`, so the loop skips it. ?string $day = Core\Str::afterPrefix($key, 'openingHours-'); if ($day == null) { echo $key, ": skipped\n"; continue; } // This prints `day 12` for the first key. echo $key, ': day ', $day, "\n";}openingHours-12: day 12name: skippedopeningHours-3: day 3openingHoursNote: skippedBearer token
<?nvs// Shows how to read the token out of an `Authorization` header.
array<string> $headers = ['Bearer abc123', 'Basic dXNlcjpwYXNz', 'bearer abc123'];
foreach ($headers as string $header) { // The first header starts with "Bearer ", so this prints `token abc123`. // The check is case-sensitive, so "bearer abc123" gives `null`. ?string $token = Core\Str::afterPrefix($header, 'Bearer '); echo $header, ': ', $token == null ? 'no token' : 'token ' . $token, "\n";}
// The prefix is the whole string, so the result is an empty string.// This prints `[]`.echo '[', Core\Str::afterPrefix('Bearer ', 'Bearer ') ?? '(none)', "]\n";Bearer abc123: token abc123Basic dXNlcjpwYXNz: no tokenbearer abc123: no token[]Whole characters
<?nvs// Shows that a prefix matches only whole characters.
// "e" followed by a combining accent (U+0301) is one character: "é".string $word = "e\u{301}cole";
// The prefix "e" ends inside that character, so the result is `null`.// This prints `(none)`.echo Core\Str::afterPrefix($word, 'e') ?? '(none)', "\n";
// The prefix "e" plus the accent is the whole character, so the cut works.// This prints `cole`.echo Core\Str::afterPrefix($word, "e\u{301}") ?? '(none)', "\n";
// `Core\Str::startsWith` gives the same answer for the same two strings.// This prints `false`.echo Core\Str::startsWith($word, 'e') ? 'true' : 'false', "\n";(none)colefalse