HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMajor

Converting snake_case to PascalCase

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
convertingsnake_casepascalcase

Problem

Is there an effective way, maybe with regex, to change the following text pattern?

I have a string like abc_def_ghi_jkl. I want to replace it to AbcDefGhiJkl. I currently use the following code to change it. Is there a more effective way than this?

implode('',array_map('ucfirst',explode('_',$string)));

Solution

More effective solution:

str_replace('_', '', ucwords($key, '_'));


From Gears library

Code Snippets

str_replace('_', '', ucwords($key, '_'));

Context

StackExchange Code Review Q#48593, answer score: 26

Revisions (0)

No revisions yet.