Created
April 19, 2018 13:07
-
-
Save finagin/1959b455adfcb2bebde90d8b9b757d9a to your computer and use it in GitHub Desktop.
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
<?php | |
$str = 'can_This User createPost'; | |
echo Str::snakeCase($str)."\n"; // can_this_user_create_org | |
echo Str::camelCase($str)."\n"; // CanThisUserCreateOrg | |
echo Str::studlyCase($str)."\n"; // canThisUserCreateOrg | |
echo Str::kebabCase($str)."\n"; // can-this-user-create-org | |
echo Str::charCase('.', $str)."\n"; // can.this.user.create.org |
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
<?php | |
class Str | |
{ | |
public static function split(string $str): array | |
{ | |
$words = preg_split('/(?:(?=[A-Z])|[-_\s])/', $str, -1, PREG_SPLIT_NO_EMPTY); | |
return array_map('strtolower', $words); | |
} | |
public static function charCase(string $glue, string $str): string | |
{ | |
return implode($glue, static::split($str)); | |
} | |
public static function snakeCase(string $str): string | |
{ | |
return static::charCase('_', $str); | |
} | |
public static function camelCase(string $str): string | |
{ | |
return implode('', array_map('ucwords', static::split($str))); | |
} | |
public static function studlyCase(string $str): string | |
{ | |
$words = static::split($str); | |
$firs = array_shift($words); | |
return $firs.implode('', array_map('ucwords', $words)); | |
} | |
public static function kebabCase(string $str): string | |
{ | |
return static::charCase('-', $str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment