Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snake_case() not working with lowercase words #15497

Closed
bradiosd opened this issue Sep 19, 2016 · 5 comments
Closed

snake_case() not working with lowercase words #15497

bradiosd opened this issue Sep 19, 2016 · 5 comments

Comments

@bradiosd
Copy link

I think snake_case() is still not working correctly; found it was last fixed here #9535.
Would fix this issue myself but don't know regex.

I expect "hello world" or "Hello World" to convert to "hello_world" regardless of if I use uppercase or lowercase. In fact, I think "HELLO WORLD" should also convert to "hello_world."

Currently it's only working if I use capitals for first letters in a sentence.

@GrahamCampbell
Copy link
Member

That seems to be correct due to the ctype_lower part.

@bradiosd
Copy link
Author

bradiosd commented Sep 19, 2016

Is there any reason why it checks that a string is not lowercase? Maybe for some other use in the framework? Personally, I wouldn't like to have to call ucfirst() or ucwords() to convert to snake case. Makes more sense it just works for all strings.

@marktopper
Copy link
Contributor

marktopper commented Sep 19, 2016

@bradbird1990: In your case I would just run this:

$string = 'HELLO WORLD';
$string = str_replace(' ', '_', strtolower($string));

echo $string; // outputs "hello_world"

Since snake_case would not be able to do this for you.

@PeterDKC
Copy link
Contributor

That seems to be correct due to the ctype_lower part.

This is incorrect. the ctype_lower() call simply skips any work if all the characters in the string are lowercase ( meaning that it's by definition already "snaked" ). Spaces, digits, or anything else will call the meat of the method.

>>> ctype_lower('Random Words Here')
=> false
>>> ctype_lower('random words here')
**=> false**
>>> ctype_lower('random')
=> true

It seems like unexpected results to return "snaked" words only if those words or letters start with uppercase and to explicitly ignore a space followed by an letter, a use case which is omitted in the tests for the method.

>>> \Illuminate\Support\Str::snake('Random words Here')
=> "randomwords_here"
>>> \Illuminate\Support\Str::snake('Random wORDS        Here')
=> "randomw_o_r_d_s_here"

As a temporary workaround under normal circumstances, one can wrap the argument in ucwords(), but this seems like a patch rather than a fix.

>>> \Illuminate\Support\Str::snake(ucwords('random words here'))
=> "random_words_here"

@PeterDKC
Copy link
Contributor

Added PR targetting [5.5] #18764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants