Skip to content

Instantly share code, notes, and snippets.

@calpo
Created April 18, 2013 03:15
Show Gist options
  • Save calpo/5409769 to your computer and use it in GitHub Desktop.
Save calpo/5409769 to your computer and use it in GitHub Desktop.
<?php
function to_snake_case($str)
{
$str = preg_replace('/([a-z0-9])([A-Z])/', '$1_$2', $str);
return strtolower(preg_replace('/([A-Z])([A-Z])([a-z0-9])/', '$1_$2$3', $str));
}
assert('hoge_fuga' == to_snake_case('HogeFuga'));
assert('qa_list' == to_snake_case('QAList'));
assert('aws_client' == to_snake_case('AWSClient'));
assert('hoge2fuga' == to_snake_case('Hoge2fuga'));
assert('hoge1_fuga2' == to_snake_case('Hoge1Fuga2'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment