Convert Dashes to CamelCase in PHP

前端 未结 25 1318
南笙
南笙 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:08

    The TurboCommons library contains a general purpose formatCase() method inside the StringUtils class, which lets you convert a string to lots of common case formats, like CamelCase, UpperCamelCase, LowerCamelCase, snake_case, Title Case, and many more.

    https://github.com/edertone/TurboCommons

    To use it, import the phar file to your project and:

    use org\turbocommons\src\main\php\utils\StringUtils;
    
    echo StringUtils::formatCase('sNake_Case', StringUtils::FORMAT_CAMEL_CASE);
    
    // will output 'sNakeCase'
    

    Here's the link to the method source code:

    https://github.com/edertone/TurboCommons/blob/b2e015cf89c8dbe372a5f5515e7d9763f45eba76/TurboCommons-Php/src/main/php/utils/StringUtils.php#L653

提交回复
热议问题