Created
December 22, 2011 12:32
-
-
Save henriquemoody/1510150 to your computer and use it in GitHub Desktop.
Camel case to separator and vice-versa.
This file contains 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 | |
/** | |
* @param string $string | |
* @param string $separator | |
* @return string | |
*/ | |
function camelCaseToSeparator($string, $separator = '-') | |
{ | |
return preg_replace('/(?<=[a-z])([A-Z])/', $separator . '$1', $string); | |
} | |
/** | |
* @param string $string | |
* @param string $separator | |
* @return string | |
*/ | |
function separatorToCamelCase($string, $separator = '-') | |
{ | |
$separator = preg_quote($separator); | |
return preg_replace("/({$separator})([a-zA-Z])/e", 'strtoupper("$2")', $string); | |
} | |
echo camelCaseToSeparator('HenriqueMoodyCarmo') . PHP_EOL; | |
echo separatorToCamelCase('henrique-moody-carmo') . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment