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

JamborJan picture JamborJan  路  3Comments

progmars picture progmars  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments