Last active
March 12, 2023 11:33
-
-
Save daverogers/16dcdc48e8dd40bd8744d18a590db68d to your computer and use it in GitHub Desktop.
Laravel's `Str::kebab()` format is basically just `Str::snake()` with the hyphen delimiter, but the downside being when you have groups of capitalized letters it'll hyphenate in between each of them instead of just before the first of the group. This isn't always desirable, like in some column name format styles...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// default Laravel output | |
Str::of('TableID')->kebab(); | |
// result: 'table-i-d' | |
// my way, grouping by capital letters | |
Str::of('TableID') | |
->replaceMatches("/([A-Z]+)/", fn($m) => '-'.Str::lower($m[0])) | |
->ltrim('-'); | |
// result: 'table-id' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment