DEV Community

Discussion on: Convert string to camel case CodeWars Kata

farhaduneci profile image
Farhad Uneci Author

Hey there,

Yes you right, this part of my write-up is a bit 😵. This is actually an example I tried to show you, this is not a part of the solution, so if I want to recap:

🔎 First I find the pattern with this regex /(\-|\_)([a-z])/i.

It will find all _ or - followed by a character, like _c or _E or -a.

✏ Second I replace all these found matches with the equivalent uppercase character; So if we have _b this will be replaced by B! This is what I meant by _b => B.

I do the replacement with the preg_replace_callback function in PHP; Inside of each iteration the $match array contains, the matched string beside regex groups!

✔ We need to uppercase the character after _ or - so I use the second group of my regex, we can have access to that group in the second index of our array so I return strtoupper($match[2]) inside the callback function.