Initially i had posted a question to find a solution to capitalize every other letter in a string. Thankfully Alex @ SOF was able to offer a great solution, however ive been unable to get it to work with an array… To be clear what im trying to do in this case is explode quotes, capitalize every other letter in the array then implode them back.
if (stripos($data, 'test') !== false) {
$arr = explode('"', $data);
$newStr = '';
foreach($arr as $index => $char) {
$newStr .= ($index % 2) ? strtolower($char) : strtoupper($char);
}
$data = implode('"', $arr);
}
Using the anonymous function requires >= PHP 5.3. If not, just make the callback a normal function. You could use
create_function(), but it is rather ugly.Output
CodePad.
If you want to swap the case, swap the
strtolower()andstrtoupper()calls.