Last active
September 7, 2018 14:13
-
-
Save friartuck6000/4f357e03377bc60552a090cbef6e94fc to your computer and use it in GitHub Desktop.
Convert snake case to camel case
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Convert a snake-cased string to a camel-cased one. | |
* | |
* @param string $snakeString The original snake-cased string. | |
* @param bool $first Whether the first letter should be capitalized | |
* (useful for classnames vs. properties). | |
* @return string The converted string. | |
*/ | |
function snake2Camel($snakeString, $first = false) | |
{ | |
$camel = implode('', array_map(function($piece) { | |
return empty($piece) ? '_' : ucfirst(strtolower($piece)); | |
}, explode('_', $snakeString))); | |
return $first ? $camel : lcfirst($camel); | |
} |
maxyc
commented
Sep 7, 2018
function snake2Camel($snakeString, $first = false)
{
if(is_numeric($snakeString))
{
return $snakeString;
}
$camel = implode(
'',
array_map(
function ($piece) {
return empty($piece) ? '_' : ucfirst(strtolower($piece));
},
explode('_', $snakeString)
)
);
return $first ? $camel : lcfirst($camel);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment