Monday, 2 February 2015

Camel Case To Under Score

Camel Case To Under Score

Here is a little helper function that is the buddy to the Underscore-To-Camel-Case function. This function does the exact opposite by taking a camelCase string and converting to under_scores.
<?php
    
/**
    *
    * Convert a camel case string to underscores (eg: camelCase becomes camel_case)
    *
    * @param    string    The string to convert
    * @return    string
    *
    */
    
function camelCaseToUnderscore$string )
    {
    
$string[0] = strtolower($string[0]);
    
$func create_function('$c''return "_" . strtolower($c[1]);');
    return 
preg_replace_callback('/([A-Z])/'$func$string);
    }
?>

USAGE

<?php
    $string 
'thisIsACamelCaseString';
    echo 
camelCaseToUnderscore$string );?>

DEMONSTRATION

this_is_a_camel_case_string

0 comments:

Post a Comment