PHP unCamelCase

To convert a string from CamelCase to underscore_notation in PHP, use the following code:

$my_camel_case_string = "fooBar";

$resulting_string = strtolower(preg_replace("[A-Z]", "_\$1", $my_camel_case_string));

$my_un_camel_cased_string = $resulting_string;

The code can be made into a function like this:

function un_camel_case($string){
  return strtolower(preg_replace("[A-Z]", "_\$1", $string));
}

One Response to “PHP unCamelCase”

Leave a Reply