Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Created November 23, 2018 19:23
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 HenriqueSilverio/e1b6b1fa147d1ff18fa23c106453b4a1 to your computer and use it in GitHub Desktop.
Save HenriqueSilverio/e1b6b1fa147d1ff18fa23c106453b4a1 to your computer and use it in GitHub Desktop.
<?php
function str_random($length = 16)
{
$string = '';
$len = strlen($string);
while ($len < $length) {
$size = $length - $len;
$bytes = random_bytes($size);
$bytes = base64_encode($bytes);
$blackList = ['/', '+', '='];
$cleanStr = str_replace($blackList, '', $bytes);
$string .= substr($cleanStr, 0, $size);
$len = strlen($string);
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment