How to convert PascalCase to pascal_case?

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

    If you are looking for a PHP 5.4 version and later answer here is the code:

    function decamelize($word) {
          return $word = preg_replace_callback(
            "/(^|[a-z])([A-Z])/",
            function($m) { return strtolower(strlen($m[1]) ? "$m[1]_$m[2]" : "$m[2]"); },
            $word
        );
    
    }
    function camelize($word) {
        return $word = preg_replace_callback(
            "/(^|_)([a-z])/",
            function($m) { return strtoupper("$m[2]"); },
            $word
        );
    
    } 
    

提交回复
热议问题