PHP Alphanumeric Random

<?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";
?>
<?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);
?>
<?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;
?>

Laissez un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors