-
-
Save platonische/9007dab590ad4ca50f7aa223c949ee24 to your computer and use it in GitHub Desktop.
Convert snake to camel case and back with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function camel_to_snake($input) | |
| { | |
| return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input)); | |
| } | |
| function snakeToCamel($input) | |
| { | |
| return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input)))); | |
| } | |
| $camel = 'CreateUserProfile'; | |
| $snake = camel_to_snake($camel); | |
| echo 'to_snake: ' . $snake . "\n"; | |
| $camel = snakeToCamel($snake); | |
| echo 'toCamel: ' . $camel . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment