Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add new acronym method to Str for retrieving acronyms from strings #46615

Closed
wants to merge 6 commits into from
Closed

[10.x] Add new acronym method to Str for retrieving acronyms from strings #46615

wants to merge 6 commits into from

Conversation

koenhendriks
Copy link

@koenhendriks koenhendriks commented Mar 28, 2023

Description

This Pull Request adds a new method to the Str helper class called acronym that retrieves an acronym consisting of the first letter of each word in a given string, concatenated without capitalization (other helper methods can be chained for this).

The acronym method can be used in a variety of contexts, such as generating abbreviations for longer phrases or creating short identifiers from longer names.

Usage

To use the acronym method, simply call the method on the Str class and pass in a string as the argument:

echo Str::acronym('Laravel is freaking everywhere');
// Output: "Life"

The acronym method splits the input string into words using spaces as a delimiter, retrieves the first letter of each word, and concatenates the resulting letters into a single string without capitalization.

An optional delimiter can be given which allows for more flexibility in how the resulting acronym is formatted.

echo Str::acronym('Laravel is freaking everywhere', '.');
// Output: "L.i.f.e."

Testing

This Pull Request includes a unit test for the acronym method to ensure that it works correctly and doesn't break any existing functionality.

@henzeb
Copy link
Contributor

henzeb commented Mar 28, 2023

Would be awesome if you can determine the delimiter in the output, so you can have results like "L.i.f.e".

@taylorotwell
Copy link
Member

You can macro this into your own project.

@koenhendriks
Copy link
Author

@taylorotwell I know I can, and I've done so in multiple projects. That's one of the reasons why I created this PR.

While it's true that you can create your own macro for generating acronyms in your own Laravel project, adding this feature to the core Str helper class in Laravel can provide benefits in terms of consistency, code reuse, and community contributions.

@henzeb
Copy link
Contributor

henzeb commented Mar 28, 2023

@koenhendriks, I suggest you make this into a package in that case.

@koenhendriks
Copy link
Author

@henzeb Seemed a bit overkill for such a small feature, but created it anyway. Should be live on packagist and can be used with

composer require koenhendriks/laravel-str-acronym

For future people landing here from search engines or github; If you want to use this functionality checkout https://github.com/koenhendriks/laravel-str-acronym

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.

None yet

4 participants