Skip to content
Advertisement

How can I convert PascalCase string to a usable slug in Laravel 7 with PHP?

I currently have a variable with the value of "ExtraOption". I wonder if it is possible (with Laravel helpers perhaps) convert this string into a usable slug. For example: "extra-option".

Can anyone help me out with a single, clean solution?

Advertisement

Answer

You can use Str::kebab() method to converts the given string to kebab-case:

use IlluminateSupportStr;

$converted = Str::kebab('ExtraOption');

// extra-option

Laravel 7 has new feature Fluent String Operations which provides a variety of helpful string manipulation functions.

kebab

The kebab method converts the given string to kebab-case:

use IlluminateSupportStr;

$converted = Str::of('ExtraOption')->kebab();

// extra-option
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement