Last active
March 7, 2019 08:10
-
-
Save enix-app/76a0c3c621505eebab7301461e44cdcf to your computer and use it in GitHub Desktop.
PHP - Start Case Function
This file contains hidden or 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 | |
if(!function_exists('str_start_case')) | |
{ | |
/** | |
* @param $str string Required. | |
* @param $replace array Optional. Default an empty array. | |
* @param $separator string Optional. Default space. | |
*/ | |
function str_start_case(string $str, array $replace=[], string $separator=" ") | |
{ | |
$str = preg_split("/(?=[A-Z])|([\s\_\-])/", ucfirst($str), -1, PREG_SPLIT_NO_EMPTY); | |
$str = array_map(function($str) use($replace) { | |
if(!empty($replace)) $str = str_replace(array_keys($replace), array_values($replace), $str); | |
return strtoupper($str[0]) . substr($str, 1); | |
}, $str); | |
return implode($separator, $str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Output: