Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created September 27, 2010 11:41
Show Gist options
  • Save yuya-takeyama/598895 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/598895 to your computer and use it in GitHub Desktop.
<?php
function camelize($input, $lower = false)
{
$result = '';
$words = explode('_', $input);
$wordCount = count($words);
for ($i = 0; $i < $wordCount; $i++) {
$word = $words[$i];
if (!($i === 0 && $lower === false)) {
$word = ucfirst($word);
} else {
$word = strtolower($word);
}
$result .= $word;
}
return $result;
}
$input = "camel_case_string";
echo camelize($input) . PHP_EOL; // => CamelCaseString
echo camelize($input, true) . PHP_EOL; // => camelCaseString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment