-
Does anyone want this String helper that creates initials of the passed string? e.g Name
I can achieve that in Vanilla PHP code using the below code
|
Beta Was this translation helpful? Give feedback.
Answered by
erikn69
Sep 1, 2022
Replies: 2 comments
-
Yes please, is this someone created yet? |
Beta Was this translation helpful? Give feedback.
0 replies
-
Feel free to make a PR, but use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
/////
Stringable::macro('initials', function(){
$words = preg_split("/\s+/", $this);
$initials = "";
foreach ($words as $w) {
$initials .= $w[0];
}
return new static($initials);
});
Str::macro('initials', function(string $string){
return (string) (new Stringable($string))->initials();
});
/* TESTS */
Str::initials('Taylor Otwell');
// TO
(string) Str::of('Taylor Otwell')->initials();
// TO |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
crynobone
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feel free to make a PR, but
Str
isMacroable