camelCase to snake_case

textmateHere is a TextMate command that will convert all of the selected text from camelCase to snake_case. This command is specifically designed for PHP. Thanks to PHP not having namespaces (until recently) many developers use PEAR naming conventions resulting in code that has class names like BP_Common::fancy_function(). This command will not convert the BP_Common.

Here is the code that you can paste in to the TextMate Bundle Editor as your new command for converting cameleCase variable to snake_case.

#!/usr/bin/env php 


One thought on “camelCase to snake_case

  1. Tom Payne says:

    Thanks for this, it was very helpful. However PREG_REPLACE_EVAL is now deprecated so I have used preg_replace_callback instead. Here’s the code I now use.

    function camel_to_snake ( $input )
     if ( preg_match ( '/[A-Z]/', $input ) === 0 ) { return $input; }
     $pattern = '/([a-z])([A-Z])/';
     $r = strtolower ( preg_replace_callback ( $pattern, function ($a) {
      return $a[1] . "_" . strtolower ( $a[2] ); 
     }, $input ) );
     return $r;



Leave a Reply

Your email address will not be published. Required fields are marked *