Skip to content

Instantly share code, notes, and snippets.

@enix-app
Last active March 7, 2019 08:10
Show Gist options
  • Save enix-app/76a0c3c621505eebab7301461e44cdcf to your computer and use it in GitHub Desktop.
Save enix-app/76a0c3c621505eebab7301461e44cdcf to your computer and use it in GitHub Desktop.
PHP - Start Case Function
<?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);
}
}
@enix-app
Copy link
Author

enix-app commented Mar 7, 2019

Usage:

<?php

$message = "welcome_to_the_jungle";

echo str_start_case($message);

Output:

Welcome To The Jungle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment