Convert Dashes to CamelCase in PHP

前端 未结 25 1198
南笙
南笙 2020-12-07 19:40

Can someone help me complete this PHP function? I want to take a string like this: \'this-is-a-string\' and convert it to this: \'thisIsAString\':

function d         


        
25条回答
  •  时光说笑
    2020-12-07 20:23

    function camelCase($text) {
        return array_reduce(
             explode('-', strtolower($text)),
             function ($carry, $value) {
                 $carry .= ucfirst($value);
                 return $carry;
             },
             '');
    }
    

    Obviously, if another delimiter than '-', e.g. '_', is to be matched too, then this won't work, then a preg_replace could convert all (consecutive) delimiters to '-' in $text first...

提交回复
热议问题