By John Koster
The snake
helper method replaces all uppercase letters within the string with the lower cased variant prefixed with the given $delimiter
. 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 $delimiter
being prefixed. Because of this behavior, the entire string will be lower cased. The snake
method trims extraneous whitespace from any string being converted.
#Signature
The signature of the snake
method is:
1public static snake(
2 $value,
3 $delimiter = '_'
4 );
#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::snake('MyWords');
5
6// my-words
7echo Str::snake('MyWords', '-');
8
9// m_y_w_o_r_d_s
10echo Str::snake('MYWORDS');
11
12// this is _pretty_cool
13echo Str::snake('this_is_PrettyCool');
#Global snake_case
Helper Function
The snake_case
function is a shortcut to calling Str::snake
. This function is declared in the global namespace.
∎