how to alter the case of a string in php? paSSw5OR

2019-02-17 10:07发布

问题:

As mentioned in this Meta Question, Facebook accepts the exact opposite case variation of our password. For example:

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

How to get the second variation, provided the first one is the original one, entered by user (or to get first one when user enters second version)? Here is my take on this.

<?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;
?>

But how to make it process passwords with special characters (all possible on a standard English keyboard?

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

This does not work on all above special characters.

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. 
}

I am no expert of RegEx, so How to modify the above RegEx to make sure it processes all the above mentioned special characters?

回答1:

Here is a function to toggle case of characters in a string .

<?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
?>

Hope this will help you

Same example is given in this link .



回答2:

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.



回答3:

you need to explore ctype_upper,ctype_lower to find upper and lowercase letters in a string then you can use strtolower and strtoupper to change the case of letters.



回答4:

Previous answers to this question only work for a-z. This works for all unicode characters.

  • ö -> Ö
  • Å -> å
  • ü -> Ü
  • and so on

    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;
    }
    


回答5:

Joakim's answer is on the right track but one line is wrong: Replace $char = $str[$i]; with $char = mb_substr($str, $i, 1); Only then will it work for all unicode characters including German.