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?
Use preg_replace_callback
:
$string = 'this-is_a-test';
function toUpper($matches) {
return strtoupper($matches[1]);
}
echo preg_replace_callback('/[-_](.)/', 'toUpper', $string); // thisIsATest
DEMO.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With