Top

Command Casing Transformation Using PHP

Need to transform text? PHP makes it a breeze.

A couple command casing functions to help you transform your text to taste

Camel Case to Snake Case

<?php
/**
* Convert a camel case string to underscores (eg: camelCase becomes camel_case)
* @param        string      The string to convert
* @return       string      The converted 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);
}
?> 

Snake Case to Camel Case

<?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);
}
?> 

I sourced this originally from phppro.org, and it has came in handy over the years! Hope it helps you.

Think I might be a good fit for your project?

Let's get the conversation started!