6

如本Meta Question中所述,Facebook 接受与我们的密码完全相反的大小写变体。例如:

1.- paSSw5ORD  (Original password)
2.- PAssW5ord  (Case altered to exact opposite, Capital->Small and vice-versa.)
3.- PaSSw5ORD  (Only first letter's case altered)

如果第一个是用户输入的原始版本,如何获得第二个变体(或者在用户输入第二个版本时获得第一个)?这是我对此的看法。

<?php

$pass = "paSSw5ORD"; //Example password
$pass_len = strlen($pass); //Find the length of string

for($i=0;$i<$pass_len;$i++){
    if(!(is_numeric($pass[$i]))){                 //If Not Number
        if($pass[$i]===(strtoupper($pass[$i]))){  //If Uppercase
            $pass2 .= strtolower($pass[$i]);      //Make Lowercase & Append
        } else{                                   // If Lowercase
            $pass2 .= strtoupper($pass[$i]);      //Make Uppercase & Append
        }
    } else{                  //If Number
        $pass2 .= $pass[$i]; //Simply Append
    }
}

//Test both
echo $pass."\r\n";
echo $pass2;
?>

但是如何让它处理带有特殊字符的密码(在标准英文键盘上都可能?

!@#$%^&*()_+|?><":}{~[];',./     (Space also)

这不适用于上述所有特殊字符。

if(preg_match('/^\[a-zA-Z]+$/', "passWORD")){
//Special Character encountered. Just append it and
//move to next cycle of loop, similar to when we
//encountered a number in above code. 
}

我不是 RegEx 专家,那么如何修改上述 RegEx 以确保它处理所有上述特殊字符?

4

5 回答 5

6

To invert the case of a string I use this function:

function invert_case($string) {
  return preg_replace('/[a-z]/ie', '\'$0\' ^ str_pad(\'\', strlen(\'$0\'), \' \')', $string);
}

I found it in the php.net manual ages ago while I was looking to do the same thing. I just turned it into a function.

于 2012-05-23T10:45:05.057 回答
3

这是一个切换字符串中字符大小写的函数。

<?php
     $string = "Hello";                                      // the string which need to be toggled case
     $string_length = strlen($string);                      // calculate the string length
     for($i=0;$i<$string_length;$i++){                        // iterate to find ascii for each character
          $current_ascii = ord($string{$i});                 // convert to ascii code
          if($current_ascii>64 && $current_ascii<91){        // check for upper case character
                 $toggled_string .= chr($current_ascii+32);   // replace with lower case character
          }elseif($current_ascii>96 && $current_ascii<123){    // check for lower case character
                $toggled_string .= chr($current_ascii-32);   // replace with upper case character
          }else{
                $toggled_string .= $string{$i};               // concatenate the non alphabetic string.
              }
        }
     echo "The toggled case letter for $string is <hr />".$toggled_string; // output will be hELLO
?>

希望对你有帮助

此链接中给出了相同的示例。

于 2012-05-23T10:50:22.037 回答
2

您需要探索ctype_upper,ctype_lower在字符串中查找大写和小写字母,然后您可以使用它strtolower and strtoupper来更改字母的大小写。

于 2012-05-23T10:40:51.990 回答
2

此问题的先前答案仅适用于 az。这适用于所有 unicode 字符。

  • ö -> Ö
  • Å -> å
  • ü -> ü
  • 等等

    function invert_case($str) {
        $inverted = '';
        for($i=0;$i<mb_strlen($str);$i++) {
            $char = $str[$i];
            $upper = mb_strtoupper($char);
            if($upper == $char) {
                $inverted .= mb_strtolower($char);
            } else {
                $inverted .= $upper;
            }
        }
        return $inverted;
    }
    
于 2012-05-23T11:24:15.197 回答
0

Joakim 的答案是正确的,但有一行是错误的:替换$char = $str[$i];$char = mb_substr($str, $i, 1); 只有这样它才能适用于包括德语在内的所有 unicode 字符。

于 2017-02-26T04:10:16.363 回答