Skip to content

Instantly share code, notes, and snippets.

@zanbaldwin
Created March 28, 2021 17:58
Show Gist options
  • Save zanbaldwin/88c7308cfbf7727415388cf39b2498f5 to your computer and use it in GitHub Desktop.
Save zanbaldwin/88c7308cfbf7727415388cf39b2498f5 to your computer and use it in GitHub Desktop.
<?php
function camelCaseToSnakeCase(string $input): string {
preg_match_all('!(^[a-z\d]+)|(?:_?)([a-z\d]+|[A-Z][a-z\d]+|[A-Z][A-Z\d]*(?=_|$|[A-Z][a-z\d]))!', $input, $matches);
return strtolower(implode('_', array_map(function (string $part): string {
return trim($part, '_');
}, $matches[0])));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment