I want to convert a dash case string (my-test-string
) to snake case (my_test_string
) with the snake_case()
helper function but it doesn't work.
The problem is that the Str:snake()
function doesn't look for dashes.
This regular expression would fix that:
/(.)(?=[A-Z]|-)/u
The snake case function only converts from camel case to snake case.
For anyone who arrived here from search, you can convert dash case (kebab case) to camel case, then from camel case to snake case (underscore case). Tested with Laravel 5.4.
$out = snake_case( camel_case( "my-test-string" ) )
@IllyaMoskvin it would if its all string. if there is numbers, it won't work, ex: sample-2
, it will only convert to camel_case which is sample2
.
Most helpful comment
For anyone who arrived here from search, you can convert dash case (kebab case) to camel case, then from camel case to snake case (underscore case). Tested with Laravel 5.4.