Search code examples
phpuppercaselowercase

Capatalize every other letter...


How would i capitalize every other letter in a string? I know how to convert to lower or upper or the first, etc but not sure how to go about every other. To be clear I've included examples. Also this is for creating a cipher my son and i are messing around with, i would never format regular text in this fashion.

pizza -> PiZzA
party -> PaRtY
popcorn -> PoPcOrN

Solution

  • $newStr = '';
    foreach(str_split($str) as $index => $char) {
        $newStr .= ($index % 2) ? strtolower($char) : strtoupper($char);
    }
    

    CodePad.