This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$characters = ["a", "b", "c", "d", "e", "f", "g", "h", | |
"i", "j", "k", "l", "m", "n", "o", "p", | |
"q", "r", "s", "t", "u", "v", "w", "x", | |
"y", "z", "A", "B", "C", "D", "E", "F", | |
"G", "H", "I", "J", "K", "L", "M", "N", | |
"O", "P", "Q", "R", "S", "T", "U", "V", | |
"W", "X", "Y", "Z", "1", "2", "3", "4", | |
"5", "6", "7", "8", "9", "0"]; | |
$length="5"; | |
$includeUpperCase = true; | |
$str = ""; | |
for ($i = 0; $i < $length; $i++) { | |
$randomNum = rand(0, count($characters)); | |
$str .= $characters[$randomNum]; | |
} | |
if (!$includeUpperCase) { | |
$str = strtolower($str); | |
} | |
echo "$str"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Randomer | |
{ | |
private $characters = ["a", "b", "c", "d", "e", "f", "g", "h", | |
"i", "j", "k", "l", "m", "n", "o", "p", | |
"q", "r", "s", "t", "u", "v", "w", "x", | |
"y", "z", "A", "B", "C", "D", "E", "F", | |
"G", "H", "I", "J", "K", "L", "M", "N", | |
"O", "P", "Q", "R", "S", "T", "U", "V", | |
"W", "X", "Y", "Z", "1", "2", "3", "4", | |
"5", "6", "7", "8", "9", "0"]; | |
/** | |
* Returns a randomly generated alphanumeric number. | |
* | |
* @param int $length the length of the new string | |
* @param bool $includeUpperCase optional parameter to determine if random string is upper case | |
* @return string the new random string | |
*/ | |
public function generateRandomString($length, $includeUpperCase = true) | |
{ | |
$str = ""; | |
for ($i = 0; $i < $length; $i++) { | |
$randomNum = rand(0, count($this->characters)); | |
$str .= $this->characters[$randomNum]; | |
} | |
if (!$includeUpperCase) { | |
$str = strtolower($str); | |
} | |
echo "$str"; | |
return $str; | |
} | |
} | |
$rdm = new Randomer; | |
$rdm->generateRandomString(18); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$characters = ["a", "b", "c", "d", "e", "f", "g", "h","i", "j", "k", "l", "m", "n", "o", "p", | |
"q", "r", "s", "t", "u", "v", "w", "x","y", "z", | |
"\'", "\$", "\"", "\^", "+", "=", "|", "%", "ù", "*", " ", "\~", "\&", | |
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", | |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", | |
"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", | |
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", | |
"\'", "\$", "\"", "\^", "+", "=", "|", "%", "ù", "*", "\~", "\&"]; | |
$length="5"; | |
$includeUpperCase = true; | |
$str = ""; | |
for ($i = 0; $i < $length; $i++) { | |
$randomNum = rand(0, count($characters)); | |
$str .= $characters[$randomNum]; | |
} | |
if (!$includeUpperCase) { | |
$str = strtolower($str); | |
} | |
echo $str; | |
?> |