Revision: 50993
Updated Code
at November 4, 2011 04:33 by TimoZachi
Updated Code
<?php /** * Function that generates a string with random caracters. * * $length: The length of the random string. * * $randomCase: If true, the generated string will also include uppercase alphabet caracters randomly. * * $includeDigits: If true, the generated string will also include digits randomly. */ function random_chars($length = 10, $randomCase = false, $includeDigits = false) { $lower = 'abcdefghjkmnpqrstuvwxyz'; $upper = 'ABCDEFGHJKMNPQRSTUVWXYZ'; $digits = '0123456789'; $chars = $lower . ($randomCase ? $upper : '') . ($includeDigits ? $digits : ''); $str = ''; $last_index = strlen($chars) - 1; for($i = 0; $i < $length; $i++) { mt_srand(hexdec(uniqid())); $str .= $chars[mt_rand(0, $last_index)]; } return $str; } //How to use $length = 20;//The string length //$random_lower: random lowercase alphabet chars, something like: rswcjanzybtaxranszxm $random_lower = random_chars($length); //$random_case: random lowercase and uppercase alphabet chars, something like: kTjkvrrejtuArxpNcPJR $random_case = random_chars($length, true); //$random_chars_digits: random lowercase and uppercase alphabet chars plus digits, something like: 9GY5KY8Q4wZ81Ge5UvDK $random_chars_digits = random_chars($length, true, true); echo $random_lower . '<br />'; echo $random_case . '<br />'; echo $random_chars_digits; ?>
Revision: 50992
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at September 10, 2011 09:31 by TimoZachi
Initial Code
<?php /** * Function that generates a string with random caracters. * * $length: The length of the random string. * * $randomCase: If true, the generated string will also include uppercase alphabet caracters randomly. * * $includeNumbers: If true, the generated string will also include digits randomly. */ function random_chars($length = 10, $randomCase = false, $includeNumbers = false) { $max = 25; if($randomCase) $max += 26; if($includeNumbers) $max += 10; $str = ''; for($i = 0; $i < $length; $i++) { $num = rand(0, $max); if($num <= 25) $num += 97; else if($max == 51) $num += 39; else if($max == 35) $num += 22; else if($max == 61) { if($num <= 51) $num += 39; else $num -= 4; } $str .= chr($num); } return $str; } //How to use $length = 20;//The string lenghth //$random_lower: random lowercase alphabet chars, something like: vdzpkmqzsjwtgxvuzqik $random_lower = random_chars($length); //$random_case: random lowercase and uppercase alphabet chars, something like: tKywtIdmyHoJGlSdqAbm $random_case = random_chars($length, true); //$random_chars_digits: random lowercase and uppercase alphabet chars plus digits, something like: pR720y3jcL7HW32yJy2I $random_chars_digits = random_chars($length, true, true); echo $random_lower . '<br />'; echo $random_case . '<br />'; echo $random_chars_digits; ?>
Initial URL
Initial Description
A simple function that generates a string with random alphabet caracters (a to z) and/or digits. Note: the string generated is not guaranteed to be unique.
Initial Title
Generate random caracters with random case and/or random digits
Initial Tags
Initial Language
PHP