Str::afterLast
Returns everything after the last occurrence of a value, or the whole string if it is absent.
Str::afterLast(string $subject, string $search): stringParameters
| Parameter | Type | Description |
|---|---|---|
$$subject | string | The string to slice. |
$$search | string | The value to search for. Matching is case sensitive and the last occurrence wins. |
Return value
stringThe remainder of $subject following the last 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.
This is the multibyte-safe counterpart to Str::after: it locates the final occurrence with mb_strrpos() and slices past it, so accented or non-Latin subjects are cut on character boundaries rather than bytes.
When to reach for it
Reach for it whenever the interesting part of a string is its last segment. The canonical case is turning a fully-qualified class name into a short name — Str::afterLast(User::class, '\\') gives 'User' — which is how Laravel itself derives things like default table and view names. The same shape covers file extensions after the last dot, the final segment of a URI path, and the leaf of a dot-notated config key.
What trips people up
Two things. First, like after, a missing search value returns the entire subject, so Str::afterLast('README', '.') is 'README' and not an empty extension — validate before you trust the result as a file type.
Second, backslashes. In a PHP single-quoted string you must write '\\' to mean one backslash, and 'App\Http' already contains a literal backslash. Getting these mixed up is the usual reason a namespace split silently returns the untouched input.
Example
When to use it
- Turn a fully-qualified class name into its short class name for a log label or a UI badge.
- Get a file extension as the text after the final dot in a filename.
- Take the leaf segment of a dot-notated config or translation key.
Frequently asked questions
How do I get the class name without the namespace in Laravel?
Str::afterLast($fqcn, '\\\\') returns the short name — remember that a single backslash is written as '\\\\' inside a PHP single-quoted string. Laravel also ships class_basename() as a global helper that does the same thing.
What is the difference between Str::after and Str::afterLast?
Str::after cuts at the first occurrence, Str::afterLast at the last. For 'a/b/c' with '/', after gives 'b/c' and afterLast gives 'c'.
Does Str::afterLast work with multibyte strings?
Yes. It uses mb_strrpos and Str::substr, so it finds the last occurrence and slices on character boundaries rather than bytes.
More examples
Short class name for a log context
The third entry has no backslash, so afterLast returns it untouched. That is exactly the behaviour you want here — an already-short name passes through — but it is the same behaviour that makes it unsafe as an extension check.
<?php
use Illuminate\Support\Str;
$jobs = [
'App\\Jobs\\SendInvoiceEmail',
'App\\Jobs\\Billing\\RetryCharge',
'ProcessUpload', // already unqualified
];
foreach ($jobs as $job) {
echo Str::afterLast($job, '\\'), "\n";
}
// SendInvoiceEmail
// RetryCharge
// ProcessUploadExtension check done safely
Wrapping afterLast in a Str::contains guard turns the "returns everything" fallback into an explicit NULL, and Str::lower normalises the case so '.PDF' and '.pdf' compare equal.
<?php
use Illuminate\Support\Str;
function extension(string $filename): ?string
{
return Str::contains($filename, '.')
? Str::lower(Str::afterLast($filename, '.'))
: null;
}
var_dump(extension('report.PDF')); // string(3) "pdf"
var_dump(extension('archive.tar.gz')); // string(2) "gz"
var_dump(extension('Makefile')); // NULL
Discussion