对于php,如何将字符串中的字符小写或大写

对于php,如何将字符串中的字符小写或大写,php,Php,我尝试了以下方法,但似乎不起作用 if ($word[$index] >= 'a' && $word[$index] <= 'z') { $word[$index] = $word[$index] - 'a' + 'A'; } else if ($word[$index] >= 'A' && $word[$index] <= 'Z') { $word[$index] = $word[$index] - 'A' + 'a'; } i

我尝试了以下方法,但似乎不起作用

if ($word[$index] >= 'a' && $word[$index] <= 'z') {
  $word[$index] = $word[$index] - 'a' + 'A';
} else if ($word[$index] >= 'A' && $word[$index] <= 'Z') {
  $word[$index] = $word[$index] - 'A' + 'a';
}

if($word[$index]>='a'&&$word[$index]='a'&&&$word[$index]如果要更改整个字符串的大小写,请尝试:
strtoupper($string)
strtolower($string)
。如果只想更改字符串第一个字母的大小写,请尝试:
ucfirst($string)
lcfirst($string)

还有
str_replace()
,它区分大小写。您可以执行类似
str_replace('a','a',$string);
的操作,将所有小写字母“a”替换为大写字母“a”


您可能需要查看一个列表。

看起来您正在尝试反转案例

$word =  strtolower($word) ^ strtoupper($word) ^ $word;

如果要反转字符串中所有字母的大小写,有一种可能的方法:

$test = 'StAcK oVeЯfLoW';
$letters = preg_split('/(?<!^)(?!$)/u', $test );
foreach ($letters as &$le) {
    $ucLe = mb_strtoupper($le, 'UTF8');
    if ($ucLe === $le) {
        $le = mb_strtolower($le, 'UTF8');
    }
    else {
        $le = $ucLe;
    }
}
unset($le); 
$reversed_test = implode('', $letters);
echo $reversed_test; // sTaCk OvEяFlOw
$test='StAcK oVeЯfLoW';

$letters=preg_split('/(?让我澄清一下:如果某个字符是大写的,你想让它变成小写,反之亦然?@raina77现在我从这个问题中得到了相同的印象。什么对你不起作用?看起来他想改变每个元素的大小写。