Here 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.
1 2 3 4 5 6 |
#!/usr/bin/env php <?php $input = file_get_contents('php://stdin'); $pattern = '/([a-z])([A-Z])/e'; $replace = "'${1}_' . strtolower('${2}')"; echo preg_replace($pattern, $replace, $input); |
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.
Thanks!
Tom.