随机字符串PHP

时间:2014-06-15 15:11:19

标签: php

我想做一个随机字符串,但我使用的是case函数,但是当我回显它时它不会返回任何值,它根本不返回任何值。 我的开关机箱功能有什么问题???

function assign_rand_value($id)
{
// accepts 1 - 36
  switch($id)
  {
    case "1":
     $rand_value = "a";
    break;
    case "2":
     $rand_value = "b";
    break;
    case "3":
     $rand_value = "c";
    break;
    case "4":
     $rand_value = "d";
    break;
    case "5":
     $rand_value = "e";
    break;
    case "6":
     $rand_value = "f";
    break;
    case "7":
     $rand_value = "g";
    break;
    case "8":
     $rand_value = "h";
    break;
    case "9":
     $rand_value = "i";
    break;
    case "10":
     $rand_value = "j";
    break;
    case "11":
     $rand_value = "k";
    break;
    case "12":
     $rand_value = "l";
    break;
    case "13":
     $rand_value = "m";
    break;
    case "14":
     $rand_value = "n";
    break;
    case "15":
     $rand_value = "o";
    break;
    case "16":
     $rand_value = "p";
    break;
    case "17":
     $rand_value = "q";
    break;
    case "18":
     $rand_value = "r";
    break;
    case "19":
     $rand_value = "s";
    break;
    case "20":
     $rand_value = "t";
    break;
    case "21":
     $rand_value = "u";
    break;
    case "22":
     $rand_value = "v";
    break;
    case "23":
     $rand_value = "w";
    break;
    case "24":
     $rand_value = "x";
    break;
    case "25":
     $rand_value = "y";
    break;
    case "26":
     $rand_value = "z";
    break;
    case "27":
     $rand_value = "0";
    break;
    case "28":
     $rand_value = "1";
    break;
    case "29":
     $rand_value = "2";
    break;
    case "30":
     $rand_value = "3";
    break;
    case "31":
     $rand_value = "4";
    break;
    case "32":
     $rand_value = "5";
    break;
    case "33":
     $rand_value = "6";
    break;
    case "34":
     $rand_value = "7";
    break;
    case "35":
     $rand_value = "8";
    break;
    case "36":
     $rand_value = "9";
    break;
  }
return $rand_value;
}

我有没有办法随机使用以下术语,尽管它已经被随机化了。

$additionalIDs = "testingjay1,testingjay2,testingjay3,testingjay4,testingjay5,testingjay6,testingjay7,testingjay8,testingjay9,testingjay10";

1 个答案:

答案 0 :(得分:1)

您不应该使用引号括起值 应该是:

case 1:

而不是:

case "1":

另外,我已经编写了生成随机字符串函数 - 这可能会帮助你:

function generate_random_string($length = 1){
    while ($length > 0){
        $whichof3options = rand (0,2);
        if ($whichof3options == 0){
            $output = $output.chr(rand(48,57)); //get random numerical chars
        }elseif ($whichof3options == 1){
            $output = $output.chr(rand(65,90)); //get random upper case alpha chars
        }elseif ($whichof3options == 2){
            $output = $output.chr(rand(97,122)); //get random lower case alpha chars
        }else{
            return false;
        }
        $length--;
    }
    if($output){
        return $output;
    }else{
        return false;
    }
}

这基本上是使用chr fucntion和rand函数创建一个随机字符,这里有一些参考:

http://us3.php.net//manual/en/function.rand.php

http://www.php.net//manual/en/function.chr.php

http://www.asciitable.com/

我将随机字符生成器封装在while循环中,这样我就可以控制要生成的字符串的长度。

此外,如果您想要更多地控制需要生成的字符串,可以进一步改进此功能。

这是一个例子。我在符号,char case等中添加了一些控件

function generate_random_string($length = 1, $symbols = false, $lowercase = true, $uppercase = true, $numerical = true){

    while ($length > 0){

        $whichof3options = rand (0,3);

        if ($whichof3options == 0 && $numerical){
            $output = $output.chr(rand(48,57)); //get random numerical chars
        }elseif ($whichof3options == 1 && $uppercase){
            $output = $output.chr(rand(65,90)); //get random upper case alpha chars
        }elseif ($whichof3options == 2 && $lowercase){
            $output = $output.chr(rand(97,122)); //get random lower case alpha chars
        }elseif ($whichof3options == 3 && $symbols){
            $output = $output.chr(rand(33,47)); //get random symbols
        }else{
            return false;
        }

        $length--;

    }

    if($output){
        return $output;
    }else{
        return false;
    }

}