Core\Str::startsWith
Core\Str::startsWith(string $s, string $prefix): boolCore\Str::startsWith( string $s, string $prefix, ): bool
Checks whether a string starts with a piece of text.
Core\Str::startsWith returns true when the first characters of the string are exactly the text you give it, and false otherwise. A text longer than the string is never at its start, so the result is false.
The check is case-sensitive, so "HTTPS://example.com" does not start with "https://". To ignore upper and lower case, change the string to lower case with Core\Str::lower first. An empty text is at the start of every string, so the result is true.
The text must also end after a whole character, the way a person reads it. "e\u{301}" is an "e" with a combining accent, and a person reads it as one character: "é". So Core\Str::startsWith("e\u{301}tude", "e") is false.
Core\Str::afterPrefix does the check and returns the rest of the string in one call. It returns null when the string does not start with the text. Core\Str::endsWith does the same check at the end of a string.
The examples below check a few beginnings, sort commands from plain text in a chat message, and send each request path to the right part of a web application.
Parameters
$sstringThe string to check.$prefixstringThe text$smust start with. The check is case-sensitive.$prefixmust end after a whole character:"e"does not match the start of"e\u{301}".
Return value
Returns bool. true when $s starts with $prefix, otherwise false. An empty $prefix is at the start of every string, so the result is true.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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 a beginning
<?nvs// Shows how to check whether a string starts with a piece of text.
string $address = 'https://example.com/shop';
// The string starts with "https://". This prints `true`.echo Core\Str::startsWith($address, 'https://') ? 'true' : 'false', "\n";
// "example" is in the string, but not at its start. This prints `false`.echo Core\Str::startsWith($address, 'example') ? 'true' : 'false', "\n";
// The check is case-sensitive. This prints `false`.echo Core\Str::startsWith($address, 'HTTPS://') ? 'true' : 'false', "\n";
// An empty text is at the start of every string. This prints `true`.echo Core\Str::startsWith($address, '') ? 'true' : 'false', "\n";truefalsefalsetrueFind the commands in a chat
<?nvs// Shows how to tell commands from plain messages in a chat.
array<string> $messages = ['/help', 'hello everyone', '/join music', 'see you at 5/6'];
foreach ($messages as string $message) { // A message is a command only when its first character is `/`. // This prints `command` for the first and third messages. if (Core\Str::startsWith($message, '/')) { echo 'command: ', $message, "\n"; } else { echo 'message: ', $message, "\n"; }}command: /helpmessage: hello everyonecommand: /join musicmessage: see you at 5/6Route a request path
<?nvs// Shows how to send each request path to the part of a web application that handles it.
class Router{ public static function areaFor(string $path): string { // The more specific beginning is checked first, so "/api/admin/" does not // count as a plain "/api/" path. if (Core\Str::startsWith($path, '/api/admin/')) { return 'admin api'; } if (Core\Str::startsWith($path, '/api/')) { return 'api'; } if (Core\Str::startsWith($path, '/assets/')) { return 'static file'; } return 'page'; }}
array<string> $paths = ['/api/orders/7', '/api/admin/users', '/assets/logo.png', '/about', '/apis'];
// "/apis" does not start with "/api/", so it is a page.foreach ($paths as string $path) { echo $path, ' -> ', Router::areaFor($path), "\n";}/api/orders/7 -> api/api/admin/users -> admin api/assets/logo.png -> static file/about -> page/apis -> page