Switch character case, php

后端 未结 9 750
难免孤独
难免孤独 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:43

    Simplest way to Toggle case is :

    echo "tOGGLE cASE : ".(strtolower($str) ^ strtoupper($str) ^ $str);
    
    0 讨论(0)
  • 2020-12-05 12:45

    OK I know you've already got an answer, but the somewhat obscure strtr() function is crying out to be used for this ;)

    $str = "Hello, My Name is Tom";
    echo strtr($str, 
               'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
               'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
    
    0 讨论(0)
  • 2020-12-05 12:46

    Very similar in function to the answer by Mark.

    preg_replace_callback(
        '/[a-z]/i',
        function($matches) {
            return $matches[0] ^ ' ';
        },
        $str
    )
    
    0 讨论(0)
提交回复
热议问题