I have the following functions:
function dashesToCamelCase($string)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $string)));
}
function camelCaseToDashes($string) {
//This method should not have Regex
$string = preg_replace('/\B([A-Z])/', '-$1', $string);
return strtolower($string);
}
And here is a Test:
$testArray = ['UserProfile', 'UserSettings', 'Settings', 'SuperLongString'];
foreach ($testArray as $testData) {
$dashed = camelCaseToDashes($testData);
$orignal = dashesToCamelCase($dashed);
echo '<pre>' . $dashed . ' | ' . $orignal . '</pre>';
}
This is the expected output:
user-profile | UserProfile
user-settings | UserSettings
settings | Settings
super-long-string | SuperLongString
Now my Question: The method camelCaseToDashes
now is using Regex
. Can you imagine a better (faster) implementation without Regex
?