koenhendriks/laravel-str-acronym
Laravel Str Acronym adds a simple Str::acronym() macro for generating acronyms and initials from strings. Use it for avatars, labels, menus, and compact display names with a clean, chainable Laravel-style API.
composer require koenhendriks/laravel-str-acronym. There is no config to publish and no migration — package discovery registers the service provider that adds the macro to Illuminate\Support\Str."koenhendriks/laravel-str-acronym": "^1.1" — do not let a composer update silently resolve to 1.2.0 and break the lock file against your framework constraint.src/ for the service provider — it's a single Str::macro('acronym', …) registration, so the source is the fastest way to confirm edge-case behaviour (multibyte input, single-word input, empty string).use Illuminate\Support\Str;
Str::acronym('Koen Hendriks'); // "KH"
Str::acronym('Laravel Str Acronym'); // "LSA"
protected function initials(): Attribute
{
return Attribute::get(fn () => Str::acronym($this->name));
}
Initials/LabelFormatter class that delegates to Str::acronym() for the base case. Don't fork the macro for one-off requirements; you'll lose upstream fixes.Str::acronym() directly in unit tests. Because the macro is registered by the provider, RefreshDatabase-style app boot is enough; you don't need feature-level HTTP tests for formatting logic.{{ Str::acronym($user->name) }} works, but for repeated use per request consider a small view component so the acronym logic lives in one place and stays testable.composer update on this package as a deliberate upgrade step: confirm laravel/framework is on 12+ before allowing ^1.2.php in composer.json matches your CI matrix before bumping, or the install will fail on the platform check rather than in your code.Str::macro('acronym', …) anywhere, it must run after the package provider boots, otherwise the package will overwrite yours. Guard with Str::hasMacro('acronym') in your own provider, or register in a booted() callback.Str::acronym() isn't in Laravel's own stubs, so PHPStan/Psalm and PhpStorm will flag it as undefined. Either add a stub file or scope an ignore to the specific call sites — don't blanket-ignore Illuminate\Support\Str calls.mb_* functions for accented or non-Latin input; if not, normalise (or transliterate) upstream before calling it. Write a test with a name containing é, ü, or a hyphenated surname.'' in an avatar fallback is a visible bug, so decide at the call site what the fallback should be.src/ once so you know exactly what you'd be reimplementing before you depend on it in production.How can I help you explore Laravel packages today?