함수 camel_case()

1 개요[ | ]

함수 camel_case()

2 PHP[ | ]

function camel_case($value) {
    $value = ucwords(str_replace(['-', '_'], ' ', $value));
    return lcfirst(str_replace(' ', '', $value));
}
$samples = ['foo_bar', 'Foo Bar', 'FooBar', 'fooBar', 'foo bar', 'foo-bar', 'Foobar', 'FOOBAR'];
foreach( $samples as $sample ) {
	echo $sample." → ".camel_case($sample) . PHP_EOL;
}

3 Python[ | ]

import re
def camel_case(s):
    s = re.sub(r"[-_ ]+","",re.sub(r'([A-Z])([a-z])',r"_\1\2",s).title())
    if s == "": return ""
    return s[0].lower() + s[1:]

samples = ['FooBar', 'fooBar', 'foo_bar', 'Foo Bar', 'foo bar', 'foo-bar', 'FOO_BAR', 'FOO-BAR', 'Foobar', 'FOOBAR', 'F', ''];
for sample in samples:
    print( '[' + sample + '] → [' + camel_case(sample) + ']' )

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}