How to convert PascalCase to pascal_case?

前端 未结 30 1674
北海茫月
北海茫月 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: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
    

提交回复
热议问题