Skip to content

Instantly share code, notes, and snippets.

@bubba-h57
Last active July 5, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bubba-h57/adddc27ebb6f951c20bc0da08ad3f09d to your computer and use it in GitHub Desktop.
Save bubba-h57/adddc27ebb6f951c20bc0da08ad3f09d to your computer and use it in GitHub Desktop.
Convert studly, traditional snake, and kebob case strings into a non-breakable snake case string.
<?php
function non breakable snake case($value, $delimiter = ' ')
{
$value = ucwords(str_replace(['-', '_'], ' ', $value));
if (! ctype_lower($value)) {
$value = preg_replace('/\s+/u', '', $value);
$value = mb_strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value), 'UTF-8');
}
return $value;
}
$studly = non breakable snake case('thisIsATest');
$snake = non breakable snake case('this_Is_A_Test');
$kebob = non breakable snake case('This-is-a-test');
printf("Studly:\t%s\nSnake:\t%s\nKebob:\t%s\n", $studly, $snake, $kebob);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment