By John Koster
The slug
helper method will format the given $title
to resemble a URI slug. It does this through a series of steps:
- Convert the
$title
to ASCII formatting (using theascii
function, along with the provided$language
); - Converts whitespace, dashes, underscores to the
$separator
; - Reduces whitespace and separators to a single character, and trims the whitespace and separators from the beginning and end of the string.
#Signature
The signature of the slug
method is:
1public static function slug(
2 $title,
3 $separator = '-',
4 $language = 'en'
5 );
#Example Use
This function will also convert the entire string to its lower cased alternative.
1use Illuminate\Support\Str;
2
3// rebuilding-stillatcom
4echo Str::slug('Rebuilding stillat.com');
5
6// customizing-the-laravel-artisan-application
7echo Str::slug(' Customizing The Laravel Artisan Application ');
8
9// laravel_artisan_config_command_the_configclear_command
10echo Str::slug(
11 'Laravel Artisan Config Command: The config:clear Command',
12 '_'
13 );
14
15// laravel_artisan_config_command_the_configclear_command
16echo Str::slug(
17 'Laravel Artisan Config Command: The config:clear Command',
18 '_',
19 'en'
20 );
#Global str_slug
Helper Function
The str_slug
function is a shortcut to calling Str::slug
. This function is declared in the global namespace.
∎