Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created July 8, 2015 16:57
Show Gist options
  • Save linxlad/bbee38a2f684e9b80d72 to your computer and use it in GitHub Desktop.
Save linxlad/bbee38a2f684e9b80d72 to your computer and use it in GitHub Desktop.
Camel case to underscore with unicode support
/**
* @param string
* @return string
*/
function camelCaseToUnderscore($string)
{
// Default patterm
$pattern = array('#(?<=(?:[A-Z]))([A-Z]+)([A-Z][A-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#');
// Check fo unicode support
$unicodeSupportEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
if ($unicodeSupportEnabled) {
// Change pattern for unicode support
$pattern = array('#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#','#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#');
}
return preg_replace_callback(
$pattern,
function ($match) {
return '_' . strtolower($match[0]);
},
lcfirst($string)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment