Str::title
Capitalises the first letter of every word and lowercases the rest.
Str::title(string $value): string- Added in Laravel 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$$value | string | The string to convert. Word boundaries are decided by mb_convert_case, which treats spaces, hyphens, underscores and most punctuation as separators. |
Return value
stringThe value with the first letter of each word uppercased and every other letter lowercased.
Changelog
- PHP 10.40Str::apa was added for real APA title casing, which leaves minor words such as 'the', 'of' and 'a' lowercase unless they start the title.
Str::title is mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'). Every word gets an uppercase first letter and a lowercase remainder, multibyte-safely.
Use it to tidy data you did not control — an all-caps import, a name typed in lowercase, a legacy column of shouting. It is a normalisation tool, not a typography tool.
What trips people up
- It lowercases the rest of each word. That is the whole surprise:
NASAbecomesNasa,iPhonebecomesIphone,McDonaldbecomesMcdonald. Never run it over text that already contains deliberate capitalisation. - Hyphens and underscores are word boundaries, so
hello-worldbecomesHello-Worldandfoo_barbecomesFoo_Bar. The separators themselves stay. - It capitalises every word, which is not how English titles are written.
Str::apaapplies the real rules and leaves minor words such asthe,ofandalowercase unless they open the title. Str::headlineis a different job again: it splits camelCase and snake_case into separate words first, so it is the one to use for turning an identifier into a label.
If you need acronyms preserved, title-case first and then restore the known exceptions with an explicit map.
Example
When to use it
- Clean an ALL CAPS name or address column imported from a legacy system.
- Normalise a search facet label so that user-entered casing does not create visual duplicates.
- Present a plain lowercase tag as a heading in a view.
Frequently asked questions
Why does Str::title turn NASA into Nasa?
Because mb_convert_case with MB_CASE_TITLE lowercases everything after each word's first letter. Restore acronyms with an explicit replacement pass after title-casing, or skip the helper for text that already has correct capitalisation.
What is the difference between Str::title and Str::apa?
Str::title capitalises every word, while Str::apa follows APA rules and keeps minor words such as 'a', 'the', 'of' and 'in' lowercase unless they begin the title. Use apa for headings and title for data normalisation.
How do I turn a camelCase field name into a readable label?
Use Str::headline, not Str::title. headline splits camelCase, snake_case and kebab-case into separate words first, so 'orderShippedAt' becomes 'Order Shipped At' where title would leave it as one word.
More examples
title, apa and headline compared
title gives "The Lord Of The Rings", apa gives "The Lord of the Rings" with the minor words correctly lowercase, and headline gives "The Lord Of The Rings" for this input but "Order Shipped At" for a camelCase identifier — which is the case headline exists for. The final dump is "Nasa Launches Artemis", the destroyed acronym.
<?php
use Illuminate\Support\Str;
$value = 'the lord of the rings';
echo 'title: ', Str::title($value), PHP_EOL;
echo 'apa: ', Str::apa($value), PHP_EOL;
echo 'headline: ', Str::headline($value), PHP_EOL;
// headline is the one that splits identifiers into words.
echo 'headline: ', Str::headline('orderShippedAt'), PHP_EOL;
// The acronym problem, in one line.
var_dump(Str::title('NASA launches ARTEMIS'));Preserving known acronyms
Prints "NASA Launches The SLS Rocket". Title-casing first and then restoring a known list is the only reliable pattern, because Str::title has no exception mechanism. Keep the acronym list short and configuration-driven; trying to detect acronyms automatically produces false positives on ordinary short words.
<?php
use Illuminate\Support\Str;
function titleKeepingAcronyms(string $value, array $acronyms): string
{
$titled = Str::title($value);
foreach ($acronyms as $acronym) {
$titled = preg_replace(
'/\b'.preg_quote(Str::title($acronym), '/').'\b/u',
$acronym,
$titled
);
}
return $titled;
}
echo titleKeepingAcronyms('nasa launches the sls rocket', ['NASA', 'SLS']), PHP_EOL;
Discussion