Skip to content

Instantly share code, notes, and snippets.

@Eddy-Barraud
Last active August 27, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eddy-Barraud/d50e9379a12d55789a8295423b3f5155 to your computer and use it in GitHub Desktop.
Save Eddy-Barraud/d50e9379a12d55789a8295423b3f5155 to your computer and use it in GitHub Desktop.
<?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;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment