递归部分无法进一步简化或美化。
但是从下划线大小写(也称为蛇箱) and 骆驼香烟盒可以通过几种不同的方式完成:
$key = 'snake_case_key';
// split into words, uppercase their first letter, join them,
// lowercase the very first letter of the name
$key = lcfirst(implode('', array_map('ucfirst', explode('_', $key))));
or
$key = 'snake_case_key';
// replace underscores with spaces, uppercase first letter of all words,
// join them, lowercase the very first letter of the name
$key = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
or
$key = 'snake_case_key':
// match underscores and the first letter after each of them,
// replace the matched string with the uppercase version of the letter
$key = preg_replace_callback(
'/_([^_])/',
function (array $m) {
return ucfirst($m[1]);
},
$key
);
选择你最喜欢的!