Skip to content

Core\Str::before

Core\Str::before(string $s, string $needle, {last?: bool}): ?stringCore\Str::before(
    string $s,
    string $needle,
    {last?: bool},
): ?string

Returns the part of a string that comes before a separator, without the separator itself.

Core\Str::before looks for the first place the separator appears and returns everything to its left. If the separator is not in the string, the result is null. If the separator is at the very start, the result is an empty string. So you can always tell "not found" apart from "found, with nothing before it". With the option { last: true }, it cuts at the last place the separator appears.

The search is case-sensitive. Core\Str::after does the same for the part on the right.

The examples below read the key out of a line, remove a file's extension with { last: true }, and take the user name out of an email address.

Parameters

  • $sstringThe string to cut.
  • $needlestringThe separator to cut at. The search is case-sensitive. The needle is not part of the result.

Options

  • lastbooloptionalWhen true, cuts at the last needle. The default is false, which cuts at the first one.

Return value

Returns ?string. The text before the needle. If the needle is not in $s, the result is null. If the needle is at the start, the result is an empty string.

Examples

Read a key

<?nvs
// Shows how to take the part of a line that comes before a separator.
// `Core\Str::before` finds the first "=" and returns the text before it.
// This prints `color`.
echo Core\Str::before('color=dark=blue', '=') ?? '(none)', "\n";
// The separator is at the start, so the result is an empty string.
// This prints `[]`.
echo '[', Core\Str::before('=dark', '=') ?? '(none)', "]\n";
// The separator is not in the string, so the result is `null`.
// This prints `(none)`.
echo Core\Str::before('no separator here', '=') ?? '(none)', "\n";
Output
color
[]
(none)

Remove an extension

<?nvs
// Shows how `{ last: true }` cuts at the last separator, to remove a file's extension.
array<string> $files = ['report.pdf', 'archive.tar.gz', 'README'];
foreach ($files as string $file) {
// With `{ last: true }`, "archive.tar.gz" gives `archive.tar`.
?string $name = Core\Str::before($file, '.', { last: true });
// "README" has no dot, so the result is `null` and `??` keeps the whole name.
echo $file, ' -> ', $name ?? $file, "\n";
}
Output
report.pdf -> report
archive.tar.gz -> archive.tar
README -> README

User name from an email

<?nvs
// Shows how to take the user name out of an email address, as a sign-up form
// does to suggest a display name.
array<string> $emails = ['ada@example.com', 'grace.hopper@example.com', 'not-an-email'];
foreach ($emails as string $email) {
?string $user = Core\Str::before($email, '@');
// An address without "@" gives `null`, so the form can show an error.
if ($user == null) {
echo $email, ": please enter a valid email address\n";
continue;
}
// This prints `ada` for the first address.
echo $email, ': hello, ', $user, "\n";
}
Output
ada@example.com: hello, ada
grace.hopper@example.com: hello, grace.hopper
not-an-email: please enter a valid email address