Skip to content

Instantly share code, notes, and snippets.

@lucablackwell
Last active November 29, 2022 10:32
Show Gist options
  • Save lucablackwell/701f2551cd35375961d8f2010a58cbd1 to your computer and use it in GitHub Desktop.
Save lucablackwell/701f2551cd35375961d8f2010a58cbd1 to your computer and use it in GitHub Desktop.
generates a string randomly from given characters, extended file functionality
<?php
# Colour functions
function blue($text) {
return "\e[;36m$text\e[0m";
}
function blue_bold($text) {
return "\e[1;36m$text\e[0m";
}
function red($text) {
return "\e[;31m$text\e[0m";
}
function yellow($text) {
return "\e[0;33m$text\e[0m";
}
function yellow_bold($text) {
return "\e[1;33m$text\e[0m";
}
function output($input, $alpha, $length) {
$output = '';
for ($i = 1; $i <= $length; $i++) {
$output .= $alpha[array_rand($alpha)];
}
return $output;
}
# Define options
$input = getopt('hmnpcis:f::l:d::', ['help', 'match', 'newline', 'progress', 'interactive', 'speed:', 'calc', 'file::', 'alpha:', 'add:', 'length:', 'delete::']);
# If no flags exist
if (empty($input)) {
echo "Use ".yellow("-h")." or ".yellow("--help")." to access the help menu.\n";
exit;
}
# Help text
if (array_key_exists('help', $input) || array_key_exists('h', $input)) {
echo "\n ".blue_bold("HELP MENU")."\nSHORT LONG DESCRIPTION\n-h --help Display this text.\n-p --progress Output the string one character at a time\n-s --speed Set the speed of individual output (higher numbers are slower)\n\n ".blue("STRING GENERATION")."\n-l --length Set length of string to generate.\nN/A --alpha Set alphabet to use (a-z by default.)\nN/A --add Add characters to default alphabet (a-z by default.)\n-m --match Enable duplicate characters.\n-c --calc Calculate the probability of the alphabet occuring in sequence. (WIP)\n\n ".blue("FILE OUTPUT")."\n-f --file Set name of output file (will create if non-existent.)\n-n --newline Write output on new line in output file.\n-d --delete Delete contents of output file (enter 'y' to delete after writing)\n\n";
}
# If interactive
if (array_key_exists('interactive', $input) || array_key_exists('i', $input)) {
# Set output length
$length = 0;
echo "Enter length:\n";
$length = readline('> ');
while ($length <= 2) {
echo "Please enter a number higher than 2.\n";
$length = readline('> ');
}
} else {
# Set output length
# If length has been chosen and isn't empty
if (array_key_exists('length', $input) && !empty($input['length'])) {
$length = $input['length'];
# If l has been chosen and isn't empty
} elseif (array_key_exists('l', $input) && !empty($input['l'])){
$length = $input['l'];
# If empty and help flags don't exist
} elseif (!empty($input) && !(array_key_exists('help', $input) || array_key_exists('h', $input))) {
echo "Please enter a length.\n";
$length = 0;
exit;
# If help flags exist
} elseif (array_key_exists('help', $input) || array_key_exists('h', $input)) {
exit;
}
# Open file
# If file has been chosen
if (array_key_exists('file', $input)) {
$filename = $input['file'];
$file_open = true;
if (empty($input['file'])) {
echo "Please enter a filename.\n";
} else {
echo "Writing to:\n".blue($filename)."\n";
}
# If f has been chosen
} elseif (array_key_exists('f', $input)){
$filename = $input['f'];
$file_open = true;
if (empty($input['f'])) {
echo "Please enter a filename.\n";
} else {
echo "Writing to:\n".blue($filename)."\n";
}
} else {
$file_open = false;
}
# Add alphabet
$dupes_found = false;
if (array_key_exists('alpha', $input) && !empty($input['alpha'])) {
$alpha = [];
$chars = str_split($input['alpha']);
foreach ($chars as $to_add) {
if (array_key_exists('match', $input) || array_key_exists('m', $input)) {
$alpha[] = $to_add;
} elseif (!in_array($to_add, $alpha)) {
$alpha[] = $to_add;
} else {
$dupes_found = true;
}
}
} elseif (array_key_exists('add', $input) && !empty($input['add'])) {
$alpha = ['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'];
$chars = str_split($input['add']);
foreach ($chars as $to_add) {
if (array_key_exists('match', $input) || array_key_exists('m', $input)) {
$alpha[] = $to_add;
} elseif (!in_array($to_add, $alpha)) {
$alpha[] = $to_add;
} else {
$dupes_found = true;
}
}
} else {
$alpha = ['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'];
//$alpha = ['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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '!', ',', '.', '-', '+', '=', '-', '_', '?', '#', '~', ':', ';'];
}
if ($dupes_found) {
echo "Duplicate letters found and skipped (enable matches to prevent.)\n\n";
}
$alpha_string = implode('', $alpha);
echo "Chosen alphabet:\n'".blue($alpha_string)."'\n\n";
# Generate string
if (array_key_exists('p', $input) || array_key_exists('progress', $input)) {
# echo letter by letter
if (array_key_exists('s', $input)) {
$speed = $input['s'];
} elseif (array_key_exists('speed', $input)) {
$speed = $input['speed'];
} else {
$speed = 66;
}
$output = '';
for ($i = 1; $i <= $length; $i++) {
$current = $alpha[array_rand($alpha)];
$output .= $current;
echo yellow($current);
usleep($speed);
}
echo "\n";
} elseif (array_key_exists('calc', $input) || array_key_exists('c', $input)) {
# Calculate probability
$output = output($input, $alpha, 100*count($alpha));
$count = substr_count($output, $alpha_string);
$divided = 100*count($alpha) / $count;
echo "$count\n$divided\n";
echo $output;
} else {
# echo full string
$output = output($input, $alpha, $length);
echo "$output\n";
}
# Write to file
if ($file_open) {
$erase_after = false;
if (array_key_exists('delete', $input)) {
if (empty($input['delete'])) {
$erase_after = false;
} else {
$erase_after = true;
}
$file = fopen($filename, 'w');
} elseif (array_key_exists('d', $input)) {
if (empty($input['d'])) {
$erase_after = false;
} else {
$erase_after = true;
}
$file = fopen($filename, 'w');
} elseif (array_key_exists('newline', $input) || array_key_exists('n', $input)) {
$file = fopen($filename, 'a');
fwrite($file, "\n");
} else {
$file = fopen($filename, 'a');
}
fwrite($file, $output);
if ($erase_after) {
$file = fopen($filename, 'w');
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment