diff options
Diffstat (limited to 'src/utils.php')
| -rw-r--r-- | src/utils.php | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/utils.php b/src/utils.php new file mode 100644 index 0000000..59ecad4 --- /dev/null +++ b/src/utils.php @@ -0,0 +1,33 @@ +<?php +function generate_random_string(int $length): string +{ + $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + $output = ""; + + for ($i = 0; $i < $length; $i++) { + $charindex = random_int(0, strlen($chars) - 1); + $output .= $chars[$charindex]; + } + + return $output; +} + +function str_safe(string $s, int|null $max_length, bool $remove_new_lines = true): string +{ + $output = $s; + + if ($remove_new_lines) { + $output = str_replace(PHP_EOL, "", $output); + } + + $output = htmlspecialchars($output); + $output = strip_tags($output); + + if ($max_length) { + $output = substr($output, 0, $max_length); + } + + $output = trim($output); + + return $output; +}
\ No newline at end of file |
