Skip to content

Instantly share code, notes, and snippets.

@piouPiouM
Created February 28, 2010 17:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piouPiouM/317702 to your computer and use it in GitHub Desktop.
Save piouPiouM/317702 to your computer and use it in GitHub Desktop.
<?php
/**
* Reverse a CamelCase string.
*
* Examples:
* uncamel('lowerCamelCase') === 'lower_camel_case'
* uncamel('UpperCamelCase') === 'upper_camel_case'
* uncamel('ThisIsAString') === 'this_is_a_string'
* uncamel('notcamelcase') === 'notcamelcase'
* uncamel('lowerCamelCase', ' | ') === 'lower | camel | case'
*
* @author Mehdi Kabab <http://pioupioum.fr/>
* @copyright 2010 Mehdi Kabab
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://pioupioum.fr/snippets/php-function-uncamel-supprimer-camel-case.html
*
* @param string $content The CamelCase string.
* @param string $separator The glue for the compound words. Defaults to '_'.
* @return string
*/
function uncamel($content, $separator = '_')
{
$content = preg_replace('#(?<=[a-zA-Z])([A-Z])(?=[a-zA-Z])#e', "'$separator' . strtolower('$1')", $content);
$content{0} = strtolower($content{0});
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment