/ Published in: PHP
                    
                                        
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.
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
<?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 = '';
for($i = 0; $i < $length; $i++)
{
}
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;
?>
Comments
 Subscribe to comments
                    Subscribe to comments
                
                