Return to Snippet

Revision: 16515
at August 13, 2009 19:46 by kirik


Updated Code
function rndstr($length=11, $type='a-zA-Z0-9')
{
	$return = $chars = null;

	if(strstr($type, 'a-z'))
		$chars .= 'abcdefghijklmnopqrstuvwxyz';
	if(strstr($type, 'A-Z'))
		$chars .= 'ABCDEFGHIJKLMNOPRQSTUVWXYZ';
	if(strstr($type, '0-9'))
		$chars .= '0123456789';

	for($i = 0, $sl = strlen($chars) - 1; $i <= $length; $i++)
		$return .= $chars[rand(0, $sl)];

	return $return;
}
/** **** Examples ****
	--- We need alphanumeric string 5 chars length ---
		echo rndstr(5);
	--- We need lowercase letters string 10 chars length ---
		echo rndstr(10, 'a-z');
	--- We need lower and upper case letters string 10 chars length ---
		echo rndstr(10, 'A-Za-z');
	--- We need numeric string 10 chars length ---
		echo rndstr(10, '0-9');
	--- ect.. ---
**/

Revision: 16514
at August 6, 2009 15:19 by kirik


Initial Code
function rndstr($length=11, $type='a-zA-Z0-9')
{
	$return = $chars = null;

	if(strstr($type, 'a-z'))
		$chars .= 'abcdefghijklmnopqrstuvwxyz';
	if(strstr($type, 'A-Z'))
		$chars .= 'ABCDEFGHIJKLMNOPRQSTUVWXYZ';
	if(strstr($type, '0-9'))
		$chars .= '0123456789';

	$sl = mb_strlen($chars) - 1;

	for($i = 0; $i < $length; $i++)
		$return .= $chars[rand(0, $sl)];

	return $return;
}
/** **** Examples ****
	--- We need alphanumeric string 5 chars length ---
		echo rndstr(5);
	--- We need lowercase letters string 10 chars length ---
		echo rndstr(10, 'a-z');
	--- We need lower and upper case letters string 10 chars length ---
		echo rndstr(10, 'A-Za-z');
	--- We need numeric string 10 chars length ---
		echo rndstr(10, '0-9');
	--- ect.. ---
**/

Initial URL

                                

Initial Description
Function for generating random strings

Initial Title
random string generator (for passwords)

Initial Tags

                                

Initial Language
PHP