Skip to content

Add string helper to get initials from a string#59230

Merged
taylorotwell merged 5 commits intolaravel:12.xfrom
denjaland:12.x
Mar 16, 2026
Merged

Add string helper to get initials from a string#59230
taylorotwell merged 5 commits intolaravel:12.xfrom
denjaland:12.x

Conversation

@denjaland
Copy link
Contributor

This PR introduces the support for retrieving the initials from a string, optionally capitalizing the string:

Str::initials('mickey mouse');       // mm
Str::initials('mickey mouse', true); // MM

It also introduces fluent API support:

new Stringable('mickey mouse')->initials();          // mm
new Stringable('mickey mouse')->initials()->upper(); // MM

Comment on lines +1471 to +1475
$parts = mb_split("\s+", $value);

$parts = array_map(fn ($part) => mb_substr($part, 0, 1), $parts);

$initials = implode('', $parts);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, you could simplify this to just

Suggested change
$parts = mb_split("\s+", $value);
$parts = array_map(fn ($part) => mb_substr($part, 0, 1), $parts);
$initials = implode('', $parts);
$initials = trim(preg_replace('/\b([[:alpha:]]).*?\b(?:\s+)?/', '$1', $value));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I agree you can probably try and go down that route, but I followed the current style of the other helpers in the class. And to me personally, the intent of what my three lines do is much more clearer to a developer than having to see what yours does. (clever is not always clearer)

Also - your version isn't multibyte safe. Running that using the string "über eats'" would return übe" whereas mine returns "üe" as per expectation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't try to be clever, I just wanted to avoid unnecessary split/implode + mapping when you just want to extract something and do not care about the rest.

Sure, the suggestion can be improved like with mb_ereg() and or replacing [[:alpha:]] with something more suiting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't say you were trying to be clever - at least that's not what I intended. I just prefer - in general - code that is immediately clear to the developer/reader of the code. It's just personal flavour/taste. No worries.

I'm just going to leave the PR as is and see whether it gets merged - feel free to suggest changes though

@taylorotwell taylorotwell merged commit 573016a into laravel:12.x Mar 16, 2026
20 of 70 checks passed
taylorotwell added a commit to laravel/docs that referenced this pull request Mar 16, 2026
* Add documentation for the new string helper initials introduced in PR laravel/framework#59230

* update docs

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants