To create a random string of x characters using defined string of possible characters we will first of all define a function which will return a random string of length that you have passed as a parameter to the function. The script is given below:
<?php
function randomStr($len) {
srand(date("s"));
$possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()";
$str="";
while(strlen($str)<$len) {
$str.=substr($possible,(rand()%(strlen($possible))),1);
}
return($str);
}
echo randomStr(8); //calling the function which will generate a random string of 8 characters.
?>
0 Comment(s)