PHPRO.ORG

Sometimes in life we are presented with strings containing underscores. If you need such a string in camelCase, this function will get you on your way.

<?php

/**
* Convert strings with underscores into CamelCase
*
* @param    string    $string    The string to convert
* @param    bool    $first_char_caps    camelCase or CamelCase
* @return    string    The converted string
*
*/
function underscoreToCamelCase$string$first_char_caps false)
{
    if( 
$first_char_caps == true )
    {
        
$string[0] = strtoupper($string[0]);
    }
    
$func create_function('$c''return strtoupper($c[1]);');
    return 
preg_replace_callback('/_([a-z])/'$func$string);
}

?>

Usage

<?php
    $str 
'this_is_under_score_word';
    echo 
underscoreToCamelCase$str);
?>

Demonstration

ThisIsUnderScoreWord