Four methods of generating string of random characters in PHP

Source: Internet
Author: User
This article to share the content is four kinds of PHP random Word Generator string method, has a certain reference value, the need for friends can refer to

Here are four basic ways to generate random strings:

Method One :

1. Generate a random integer in 33–126, such as 35.

2, convert 35 to the corresponding ASCII code characters, such as 35 corresponding #.

3, repeat the above 1, 2 steps n times, connected to n-bit password.

The algorithm mainly uses two functions, Mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max the range of ASCII code, here take 33-126, you can adjust the range as necessary, such as the ASCII code table 97– 122 digits corresponding to the a–z of the English alphabet, specific reference to the ASCII code table; the Chr (int $ascii) function is used to convert the corresponding integer $ascii to the corresponding character.

function Create_password ($PW _length =              {                     $randpwd = ";                     for ($i = 0; $i < $PW _length; $i + +)                     {                         $randpwd. = Chr (Mt_rand (126));                     }                             return $randpwd;               }                 Call this function, pass length parameter $pw_length = 6             echo create_password (6);


Method Two :


1, preset a string $chars, including A–z,a–z,0–9, as well as some special characters.

2. Randomly take a character in the $chars string.

3, repeat the second step n times, can get the length of the password of N.

function Generate_password ($length = 8)        {                 //password character set, you can add any character you need                        $chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_                                     []{}<>~ ' +=,.;:/?| ';                           $password = ";                           for ($i = 0; $i < $length; $i + +)          {/                              /provides two ways to get                                   a character here///The first is to use SUBSTR to intercept any one character in $chars;                                   //The second is to take a character array $c Any element of the HARs           //$password. = substr ($chars, Mt_rand (0, strlen ($chars) –1), 1);           $password. = $str {Mt_rand (0,strlen ($chars)-1)};              Generate PHP random number            $password. = $chars [Mt_rand (0, strlen ($chars)-1)];             }             return $password;            }


Method Three :


1, preset a character array $chars, including a–z,a–z,0–9, as well as some special characters.

2. $length elements are randomly selected from the array $chars by Array_rand ().

3, according to the obtained key an array group $keys, remove the character concatenation string from the array $chars. The disadvantage of the method is that the same characters are not repeated.

 function Make_password ($length = 8) {//password character set, you can add any character you need $chars = Array (' A ', ' B ', ' C ', ' d ', ' E ', ' f ', ' g ', ' h ', I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W '                 , ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ',                  ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0′, ' 1′, ' 2′, ' 3′, ' 4′, ' 5′, ' 6′, ' 7′, ' 8′, ' 9′, '! ', ' @ ', ' # ', ' $ ', '% ', ' ^ ', ' & ', ' * ', ' (', ') ', '-', ' _ ', ' [', '] ', ' {', '} ', ' < ', ' &G                t; ', ' ~ ', ' ', ' + ', ' = ', ', ', '. ', '; ', ': ', '/', '? ', ' | ';             Randomly take $length number of element key names in the $chars $keys =array_rand ($chars, $length);            $password = "; for ($i = 0; $i < $length; $i + +) {//To concatenate $length array elements into string $password. = $chars [$ke           ys[$i]];     } return $password;     } 

method Four :

Basic ideas MD5 encrypt and generate 32-bit strings
Mt_rand generating random numbers for a specified range
Time to get timestamp
A random number with a random number + the current timestamp can be used to obtain a less repetitive

The code is as follows:

$rand = MD5 (Time (). Mt_rand (0,1000));

add : The PHP variable is followed by a curly brace {}, which is filled with numbers, which refers to the character of the corresponding ordinal of the PHP variable.

For example:
$str = ' Hello ';
echo $str {0}; Output is H
echo $str {1}; Output to E
If you want to check how much length a string satisfies, consider replacing the strlen function with this curly brace (curly brace) and isset, because Isset is a language structure and strlen is a function, so using isset is more efficient than using strlen.
For example, determine whether a string is less than 5 in length:
if (!isset ($str {5})) is better than if (strlen ($STR) < 5).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.