How to convert PascalCase to pascal_case?

前端 未结 30 1473
北海茫月
北海茫月 2020-11-29 16:36

If I had:

$string = \"PascalCase\";

I need

\"pascal_case\"

Does PHP offer a function for this purpose?

相关标签:
30条回答
  • 2020-11-29 17:15
    $str = 'FooBarBaz';
    
    return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $str)); // foo_bar_baz
    
    0 讨论(0)
  • 2020-11-29 17:16

    The Symfony Serializer Component has a CamelCaseToSnakeCaseNameConverter that has two methods normalize() and denormalize(). These can be used as follows:

    $nameConverter = new CamelCaseToSnakeCaseNameConverter();
    
    echo $nameConverter->normalize('camelCase');
    // outputs: camel_case
    
    echo $nameConverter->denormalize('snake_case');
    // outputs: snakeCase
    
    0 讨论(0)
  • 2020-11-29 17:16

    This is one of shorter ways:

    function camel_to_snake($input)
    {
        return strtolower(ltrim(preg_replace('/([A-Z])/', '_\\1', $input), '_'));
    }
    
    0 讨论(0)
  • 2020-11-29 17:17

    "CamelCase" to "camel_case":

    function camelToSnake($camel)
    {
        $snake = preg_replace('/[A-Z]/', '_$0', $camel);
        $snake = strtolower($snake);
        $snake = ltrim($snake, '_');
        return $snake;
    }
    

    or:

    function camelToSnake($camel)
    {
        $snake = preg_replace_callback('/[A-Z]/', function ($match){
            return '_' . strtolower($match[0]);
        }, $camel);
        return ltrim($snake, '_');
    }
    
    0 讨论(0)
  • 2020-11-29 17:17

    The open source 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('camelCase', StringUtils::FORMAT_SNAKE_CASE);
    
    // will output 'camel_Case'
    
    0 讨论(0)
  • 2020-11-29 17:18

    How to de-camelize without using regex:

    function decamelize($str, $glue = '_') {
        $capitals = [];
        $replace  = [];
    
        foreach(str_split($str) as $index => $char) {
            if(!ctype_upper($char)) {
                continue;
            }
    
            $capitals[] = $char;
            $replace[]  = ($index > 0 ? $glue : '') . strtolower($char);
        }
    
        if(count($capitals) > 0) {
            return str_replace($capitals, $replace, $str);
        }
    
        return $str;
    }
    

    An edit:

    How would I do that in 2019:

    function toSnakeCase($str, $glue = '_') {
        return preg_replace_callback('/[A-Z]/', function ($matches) use ($glue) {
            return $glue . strtolower($matches[0]);
        }, $str);
    }
    

    And when PHP 7.4 will be released:

    function toSnakeCase($str, $glue = '_') {
        return preg_replace_callback('/[A-Z]/', fn($matches) => $glue . strtolower($matches[0]), $str);
    }
    
    0 讨论(0)
提交回复
热议问题