English 中文(简体)
如何将 php? paSSw5ORD 中的字符串框更改为 PassW5ord?
原标题:how to alter the case of a string in php? paSSw5ORD to PAssW5ord?

如此 < a href=" https://webapps.stackschange.com/ questions/26301/facebook-acceptions-more-than-one-password" 所述,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)

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."
";
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以确保它处理上述所有特殊人物?

最佳回答

在此设定一个函数, 以切换字符串中字符的大小写 。

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

希望这能帮到你

此 < a href=" "http://mydons.com/how-to-toggle-criacter-case- in-php-unse-strlower-strupper-uc-first-inst-and-array- conference/" "rel=" "nofollow" >link 。

问题回答

要反转字符串的大小写,我使用此函数:

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

我在很久以前的 http://www.php.net/ manual/en/formation.str-shuffle.php#55509>> php.net 手册 中找到它,而我正想做同样的事情。我只是把它变成一个函数。

您需要探索 ctype_upper, ctype_lower 以在字符串中找到上写和小写字母,然后您就可以使用 strower和strtoupper 来更改字母的大小写 。

这个问题之前的答案只对 a-z 有效。 这对所有 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;
    }
    

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.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签