如何更改php中的字符串的大小写? paSSw5ORD到PAssW5ord? [英] how to alter the case of a string in php? paSSw5ORD to PAssW5ord?

查看:104
本文介绍了如何更改php中的字符串的大小写? paSSw5ORD到PAssW5ord?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

元问题所述,Facebook接受确切的相反的情况我们的密码变化。例如:

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

但是如何使用特殊字符处理密码(所有这些都可能在标准的英文键盘上? p>

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

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

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

推荐答案

这是一个函数来切换字符串中的字符。

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

希望这将有助于您

例子在这里链接

这篇关于如何更改php中的字符串的大小写? paSSw5ORD到PAssW5ord?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆