How to convert PascalCase to pascal_case?

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

    This is one of shorter ways:

    function camel_to_snake($input)
    {
        return strtolower(ltrim(preg_replace('/([A-Z])/', '_\\1', $input), '_'));
    }
    

提交回复
热议问题