Search

Laravel 5.5 String Helper Method: kebab

November 30, 2017 —John Koster

The snake helper method replaces all uppercase letters within the string with the lower cased variant prefixed with the - character. The only exception to this rule is that if the first character of the string is capitalized, it will be replaced by the lower cased variant without the - character being prefixed. Because of this behavior, the entire string will be lower cased. The kebab method trims extraneous whitespace from any string being converted.

#Signature

The signature of the snake method is:

1public static kebab(
2 $value
3 );

#Example Use

Here are a few examples with the result above the function call in comments:

1use Illuminate\Support\Str;
2 
3// my-words
4echo Str::kebab('MyWords');
5 
6// m-y-w-o-r-d-s
7echo Str::kebab('MYWORDS');
8 
9// The kebab method produces the same output as the snake method
10// when using the '-' as the delimiter. The following example
11// would produce the same output as the kebab method:
12echo Str::snake('MYWORDS', '-');
13 
14// this_is_-pretty-cool
15echo Str::kebab('this_is_PrettyCool');

The kebab helper method is simply a wrapper around the snake helper method. Calling kebab($value) is equivalent to calling snake($value, '-').

#Global kebab_case Helper Function

The kebab_case function is a shortcut to calling Str::kebab. This function is declared in the global namespace.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.