Switch character case, php

后端 未结 9 863
难免孤独
难免孤独 2020-12-05 11:48

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

9条回答
  •  隐瞒了意图╮
    2020-12-05 12:41

    If your string is ASCII only, you can use XOR:

    $str = "Hello, My Name is Tom";
    
    print strtolower($str) ^ strtoupper($str) ^ $str;
    

    Outputs:

    hELLO, mY nAME IS tOM
    

提交回复
热议问题