Str::before

Returns everything before the first occurrence of a value, or the whole string if it is absent.

Signature
Str::before(string $subject, string $search): string

Parameters

ParameterTypeDescription
$$subjectstringThe string to slice.
$$searchstringThe value to search for. Matching is case sensitive and only the first occurrence is used.

Return value

stringThe portion of $subject preceding the first occurrence of $search. If $search is an empty string, or does not appear in $subject, the entire $subject is returned unchanged.

Errors

Nothing. A missing search value is a normal result, not an error.

Str::before is strstr($subject, $search, true) with a fallback: when strstr reports no match it returns the untouched subject instead of false. It cuts at the first occurrence, so Str::before('2026-09-03', '-') is '2026'.

When to reach for it

Use it to take the head of a delimited string: the local part of an email address, a path without its query string, the key half of key=value, the first line of a multi-line block. It pairs naturally with Str::after when you want both halves.

What trips people up

The delimiter is removed but surrounding whitespace is not. The documented example, Str::before('This is my name', 'my name'), returns 'This is ' — with a trailing space that will show up as a stray gap in your UI or break an array key comparison. Wrap it in trim() whenever the result becomes a key, a label or a lookup value.

As with after, a search value that is missing gives you the whole subject back. Written as $user = Str::before($input, '@'), a malformed address quietly becomes a plausible-looking username.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Take the local part of an email address, before the @.
  • Drop a query string by cutting a URL at the first ? character.
  • Read the key half of a "key=value" configuration line.

Frequently asked questions

Why does Str::before leave a trailing space?

Because it removes only the search value, not the whitespace around it. Str::before('This is my name', 'my name') is 'This is ' — call trim() on the result if the space matters.

How do I get the text before the last occurrence?

Use Str::beforeLast(), which cuts at the final match. Str::before always cuts at the first, so '2026-09-03' with '-' gives '2026'.

What does Str::before return when the search value is missing?

The entire subject, unchanged. There is no null or false return, so guard with Str::contains when a missing delimiter should be handled differently.

More examples

Splitting a string into both halves

before and after read the two halves around the same delimiter. The Str::contains guard is what stops the malformed line from producing a key of 'MALFORMED' with an identical value, which is what the bare helpers would have returned.

Example · php
<?php

use Illuminate\Support\Str;

$lines = ['APP_ENV=production', 'APP_DEBUG=false', 'MALFORMED'];

foreach ($lines as $line) {
    if (! Str::contains($line, '=')) {
        echo "skipping: {$line}\n";
        continue;
    }

    $key   = trim(Str::before($line, '='));
    $value = trim(Str::after($line, '='));

    echo "{$key} => {$value}\n";
}

// APP_ENV => production
// APP_DEBUG => false
// skipping: MALFORMED

Dropping a query string

Both inputs normalise to the same path. Here the "return everything when not found" behaviour is exactly what you want, because a URL with no query string is already the bare path.

Example · php
<?php

use Illuminate\Support\Str;

$urls = [
    '/products/42?ref=newsletter&utm_source=x',
    '/products/42',
];

foreach ($urls as $url) {
    echo Str::before($url, '?'), "\n";
}

// /products/42
// /products/42

Discussion

  • Be the first to comment on this lesson.