Last active
October 27, 2019 05:01
-
-
Save robertdrakedennis/d58153716e1dedb920d91f935307818c to your computer and use it in GitHub Desktop.
Helper for generating a truly random string
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 | |
if (! function_exists('randomize')) { | |
/** | |
* Generates a random string used for various simple randomness. | |
* | |
* @param int $min | |
* @param int $max | |
* @param int $limit | |
* @return string | |
* @throws Exception | |
*/ | |
function randomize(int $min = 10, int $max = 16, int $limit = null) | |
{ | |
$randomized = \hash('sha256', \random_bytes(\rand($min, $max))); | |
if ($limit !== null) | |
{ | |
return (string) \Illuminate\Support\Str::limit($randomized, $limit, ''); | |
} | |
return (string) $randomized; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can limit the string if you want it for things like unique slugs.
Example:
Doing this will allow you for users to have the same title / slug without having any real conflicts or downsides, users should be allowed to have the same thread title especially when it's in a board like general etc