I have the following string in PHP:
this-is_a-test
I want to change that to this:
thisIsATest
So the string can contain any numer of dashes or underscores. I need some regex function that changes the string to a camel case string.
How can this be done?
$string = 'this-is_a-test';
function toUpper($matches) {
return strtoupper($matches[1]);
}
echo preg_replace_callback('/[-_](.)/', 'toUpper', $string); // thisIsATest
DEMO.
Nah, you don't need regex for that.
str_replace()
to replace the punctuation with spaces.ucwords()
to upper-case the first letter of each word.str_replace()
again to get rid of the spaces.You could use regex, but it's not necessary.