Skip to content

Instantly share code, notes, and snippets.

@platonische
Forked from carousel/snake-to-camel.php
Created March 15, 2024 04:44
Show Gist options
  • Select an option

  • Save platonische/9007dab590ad4ca50f7aa223c949ee24 to your computer and use it in GitHub Desktop.

Select an option

Save platonische/9007dab590ad4ca50f7aa223c949ee24 to your computer and use it in GitHub Desktop.
Convert snake to camel case and back with PHP
<?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