DEV Community

Discussion on: Convert string to camel case CodeWars Kata

Collapse
 
kaungpyaeminthein profile image
Kaung Pyae Min Thein

I understand the Idea of the solution until here-> "B so the-stealth will be theStealth." . But I don't understand the code and follow info . No offense. May be I don't understand PHP well or I am stupid.......

Collapse
 
feriun profile image
Farhad

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.