Skip to content

Instantly share code, notes, and snippets.

@friartuck6000
Last active September 7, 2018 14:13
Show Gist options
  • Save friartuck6000/4f357e03377bc60552a090cbef6e94fc to your computer and use it in GitHub Desktop.
Save friartuck6000/4f357e03377bc60552a090cbef6e94fc to your computer and use it in GitHub Desktop.
Convert snake case to camel case
<?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
Copy link

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