Created
July 8, 2015 16:57
-
-
Save linxlad/bbee38a2f684e9b80d72 to your computer and use it in GitHub Desktop.
Camel case to underscore with unicode support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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