Skip to content

Instantly share code, notes, and snippets.

@daverogers
Last active March 12, 2023 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daverogers/16dcdc48e8dd40bd8744d18a590db68d to your computer and use it in GitHub Desktop.
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...
<?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