For anyone interested in converting snake_case_strings into CamelCaseStrings, here’s a quick php snippet to do it:

/**
* Take a string_like_this and return a StringLikeThis
*
* @param string
* @return string
*/
function _snakeToCamel($val) {
return str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
}

Or if you prefer the inline thing…

$val = 'snake_case_string';
$val = str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
echo $val; // Output: SnakeCaseString

Update: Back To CamelCaseString

The popularity of this post has prompted the natural question on how to go from snake_case_string to the camel case version – SnakeCaseString. This gives me the opportunity to make use of a little known feature of php, the create_function function which allows you to make inline, anonymous, functions and use them as parameters. Take a look at this function to see it in use.

function _camelToSnake($val) {
return preg_replace_callback('/[A-Z]/',
create_function('$match', 'return "_" . strtolower($match[0]);'),
$val);
}

ANOTHER UPDATE: Some people want to get a stringLikeThis rather than a StringLikeThis – keeping the first letter lowercase. This is actually a little trickier to accomplish because there is no lcfirst() function like there is a ucfirst() function [NOTE: lcfirst() is coming in PHP 5.3]. So here is an updated function that returns your camelCaseValue starting with a lowercase letter.

function _snakeToCamel($val) {
$val = str_replace(' ', '', ucwords(str_replace('_', ' ', $val)));
$val = strtolower(substr($val,0,1)).substr($val,1);
return $val;
}