Return to Snippet

Revision: 58049
at June 22, 2012 08:00 by _reydin_


Initial Code
<?

$l='a';					//Set the starting letter
for($i=0;$i<26;$i++){	//Cycle through all of the letters of the alphabet a-z
	$key[$i]=$l;		//Load the array with lowercase variables at position $i
	$l++;				//Increment the letter
}
for($x=0;$x<5;$x++){	//Cycle through this five times. The more you cycle through this loop the more likely a number will appear.
	for($n=0;$n<10;$n++){	//Cycle through the numbers 0-9
		$key[$i]=$n;	//Load the array with number variables at position $i
		$i++;			//Increment the array key
	}
}
$u='A';					//Set the starting letter
for($c=0;$c<26;$c++){	//Cycle through all of the letters of the alphabet A-Z
	$key[$i]=$u;		//Load the array with uppercase variables
	$i++;				//Increment the array key
	$u++;				//Increment the letter
}

$i--;	//Subtract the last incrementation of $i because it's not indexed

$str='';
for($strlength=0;$strlength<20;$strlength++){	//Generate the random string 20 characters long
	//$r = rand(0,$i);	//Generate a random number from the available $i interger
	$r = rand()&$i;		//Generate a random number from the available $i interger using bitwise
	$str .= $key[$r];	//Concatenate the random variable
}

echo $str;	//Show the random string
?>

Initial URL


Initial Description
This will generate a random alpha numeric string 20 characters long.

Initial Title
Php Random String Generator

Initial Tags
php

Initial Language
PHP