Framework: The `snake_case()` helper function doesn't work with dash case strings

Created on 5 Aug 2016  ·  3Comments  ·  Source: laravel/framework

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

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.

$out = snake_case( camel_case( "my-test-string" ) )

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

lzp819739483 picture lzp819739483  ·  3Comments

Fuzzyma picture Fuzzyma  ·  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  ·  3Comments

JamborJan picture JamborJan  ·  3Comments