Generating a random numbers and letters

前端 未结 15 1090
梦如初夏
梦如初夏 2020-12-25 08:23

Can I ask for a certain code on how to generate a random letters and numbers in one word. I know there\'s a PHP function rand(), but I don\'t know if it\'s also applicable w

相关标签:
15条回答
  • 2020-12-25 08:41
    <?php
    echo substr(sha1(mt_rand()),17,6); //To Generate Random Numbers with Letters.
    ?>
    
    0 讨论(0)
  • 2020-12-25 08:42
    // A string with random letters and numbers. A-Z, a-z, 0-9
    
    
    // A function in PHP is a block of code that can be used elsewhere in code.
    // This function is called rand_string and will generate a random sequence of characters in one string.
    // A default value of 16 characters is set, so that if no integer is supplied it will use the value of 16.
    function rand_string($length = 16) {
    
        // A string is something that holds alphanumeric characters and other symbols. 
        // This string is an empty one, or at least that's how it starts.
        $string = '';
    
        // This is known as a for/next loop, it's will run a section of code for a set number of times.
        // A counter $i is incremented on each pass. In this case until it has operated $length number of times.
        for ($i = 0; $i < $length; $i++) {
    
            // This variable ($die) is assigned a random number - which is obtained via the PHP function mt_rand.
            //Consult the PHP docs for more information.
            $die = mt_rand(1, 3);
    
            // This switch statement picks a case that is true and runs the accompanying code as defined in each case.
            switch ($die) {
    
                // This case will be activated if the variable $die has the value of 1. And case 2 if it has the value of 2 and so on.
                case 1:
                    // Here and subsequently a random value between 48 and 57 is assigned to the $rnd variable.
                    $rnd = mt_rand(48, 57);
                    break;
    
                case 2:
                    $rnd = mt_rand(65, 90);
                    break;
    
                case 3:
                    $rnd = mt_rand(97, 122);
                    break;
            }
    
            // This is another variable $string which is assigned the ASCII character that is represented by the $rnd variable.
            // ASCII characters are codes that computers use to represent characters and symbols.
            // The chr function is a special PHP function that returns the character represented by the ASCII code.
            // In this case the value of $rnd.
            $string .= chr($rnd);
        }
    
        // Here we reach the final result. The value of $string is returned to source of the function call.
        return $string;
    }
    
    // Segments of the function, loops and switches are enclosed between curly brackets {}. This limits the scope of the processing contained within.
    
    // Usage of this function to obtain a 10 character random string.
    // echo is a function that prints the result to the browser/screen.
    $mystring = rand_string(10);
    echo $mystring;
    
    0 讨论(0)
  • 2020-12-25 08:43

    This was an easy way for me- this generates a 15 character string -

            $alph = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            $code='';
    
            for($i=0;$i<15;$i++){
               $code .= $alph[rand(0, 35)];
            }
    
    0 讨论(0)
  • 2020-12-25 08:44
    echo bin2hex(openssl_random_pseudo_bytes(12));
    
    0 讨论(0)
  • 2020-12-25 08:45

    This is a more extensive method that I use constantly to generate random numbers, letters or mixed:

    function assign_rand_value($num) {
    
        // accepts 1 - 36
        switch($num) {
            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;
    }
    
    function get_rand_alphanumeric($length) {
        if ($length>0) {
            $rand_id="";
            for ($i=1; $i<=$length; $i++) {
                mt_srand((double)microtime() * 1000000);
                $num = mt_rand(1,36);
                $rand_id .= assign_rand_value($num);
            }
        }
        return $rand_id;
    }
    
    function get_rand_numbers($length) {
        if ($length>0) {
            $rand_id="";
            for($i=1; $i<=$length; $i++) {
                mt_srand((double)microtime() * 1000000);
                $num = mt_rand(27,36);
                $rand_id .= assign_rand_value($num);
            }
        }
        return $rand_id;
    }
    
    function get_rand_letters($length) {
        if ($length>0) {
            $rand_id="";
            for($i=1; $i<=$length; $i++) {
                mt_srand((double)microtime() * 1000000);
                $num = mt_rand(1,26);
                $rand_id .= assign_rand_value($num);
            }
        }
        return $rand_id;
    }
    

    USAGE:

    Basically I have a main function with the array, then I call secondary functions to build my string based on the length parameter:

    Letters:

    $str = get_rand_letters(8); // Only Letters
    

    Numbers:

    $str = get_rand_numbers(8); // Only Numbers
    

    AlphaNumeric:

    $str = get_rand_alphanumeric(8); // Numbers and Letters
    

    This Question is answered by Kerrek SB, but this may help someone searching for a more extensive and flexible way.

    0 讨论(0)
  • 2020-12-25 08:45

    Easiest way to do this with full control:

    <?php
    $str = 'QaR0SbT1UcV2WdX3YeZ4f5g6h7i8j9kAlBmCnDoEpFqGrHsItJuKvLwMxNyOzP';
    // a-z 0-9 A-Z in above string
    
    $shuffled = str_shuffle($str);
    
    $shuffled = substr($shuffled,1,30);
    
    echo $shuffled;
    ?>
    

    Change $str according to your needs. I used small alphabets a to z, capital alphabets A to Z and numeric values 0 to 9.

    0 讨论(0)
提交回复
热议问题