Invert case of all letters in a string (uppercase to lowercase and lowercase to uppercase)

case, php, string

Solution

You'll need to iterate through the string testing the case of each character, calling `strtolower()` or `strtoupper()` as appropriate, adding the modified character to a new string.

Problem

How can I swap around / toggle the case of the characters in a string, for example: ``` $str = "Hello, My Name is Tom"; ``` After I run the code I get a result like this: ``` $newstr = "hELLO, mY nAME Is tOM"; ``` Is this even possible?

Original source

Related problems