I need to create a auto incrementing string similar to this randomly via php. The string is like this. So what i need is a mix of numbers and letter that is randomally generated and doesnt have to be sequential just as long as its random like this 823N9823 and it has 8 cha开发者_运维技巧racters max
If the characters don't need to be unique:
function randomString($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
$randomString = '';
$numofChars = strlen($chars);
while (--$length) {
$randomString .= $chars[mt_rand(0, $numofChars - 1)];
}
return $randomString;
}
If the characters must be unique:
function randomString($length = 8, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') {
return substr(str_shuffle($chars), 0, 8);
}
usage:
echo generateRandomName();
function:
function generateRandomName($length=8,$level=2){
list($usec, $sec) = explode(' ', microtime());
srand((float) $sec + ((float) $usec * 100000));
$validchars[1] = "0123456789abcdfghjkmnpqrstvwxyz";
$validchars[2] = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$validchars[3] = "0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%&*()-=+/";
$password = "";
$counter = 0;
while ($counter < $length) {
$actChar = substr($validchars[$level], rand(0, strlen($validchars[$level])-1), 1);
// All character must be different
if (!strstr($password, $actChar)) {
$password .= $actChar;
$counter++;
}
}
return $password;
}
Found on PHPToys
Level the level of characters to be used, 3 have special chars as $validchars[3]
has.
You also can call it as: generateRandomName(5)
to generate a name with length of 5
.
function generateRandomString($length=8, $characters='abcdefghijklmnopqrstuvwxyz0123456789'){
$str = '';
$len = strlen($characters);
for($i=0; $i<length; $i++){
$str .= $characters[mt_rand(0, $len)];
}
return $string;
}
Usage:
//generates a random string, using defaults: length = 8 and characters = a-z, 0-9
echo "Your password: " . generateRandomString();
//custom length: 10
echo "Thingeys: " . generateRandomString(10);
//digits only, length 4
echo "Your PIN: " . generateRandomString(4, '0123456789');
You can use mt_rand()
to generate a random integer and then transform the values into a string. The function below will give you 2 821 109 907 456
possible values.
function random_id($length = 8) {
$validChars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$rndString = '';
for($i = 0; $i < $length; $i++) {
$rndString .= $validChars[mt_rand(0, strlen($validChars) - 1)];
}
return $rndString;
}
$lengthEight = random_id();
$lengthTen = random_id(10);
Since there are more letters and numbers in the list of possible characters, you'll usually get a string with more letters then numbers.
If you want to skew the results towards a string with more numbers then letters, then you can use the following:
function skewed_random_id($numericFactor = 0.8, $length = 8) {
$validAlpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$validNumeric = '0123456789';
$rndString = '';
for($i = 0; $i < $length; $i++) {
if((mt_rand() / mt_getrandmax()) < $numericFactor) {
$rndString .= $validNumeric[mt_rand(0, strlen($validNumeric) - 1)];
} else {
$rndString .= $validAlpha[mt_rand(0, strlen($validAlpha) - 1)];
}
}
return $rndString;
}
$eightyPercentNumeric = skewed_random_id();
$fiftyFifty = skewed_random_id(0.5);
$allAlpha = skewed_random_id(0);
$allNumeric = skewed_random_id(1);
echo substr(str_replace(array('/', '+'), '', base64_encode(sha1(uniqid(), true))), 0, 8);
Sample output:
LTeQpy9p
v11oy9R7
TjdkXZru
lh3TvNi4
X5Ca0ZXS
L01RfhxW
QDfwNnGO
you can use strtoupper
and strtolower
to get a version that suits your needs.
EDIT:
If you are gonna use this for something critical rather than just wanting a random string, change uniqid()
into mt_rand()
:
echo substr(str_replace(array('/', '+'), '', base64_encode(sha1(
mt_rand(). '.'. mt_rand(). '.'. mt_rand()%4 //256^8 of input possibilities (8 bytes)
, true))), 0, 8);
substr(uniqid(), 0, 8);
http://php.net/uniqid
精彩评论