Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
generate random string via php - used https://packagist.org/packages/voku/portable-utf8 (UTF8::strlen)
<?php
/**
* generate random string
*
* @param string $len length of the random string
* @param string $characters characters string for the random selection
*
* @return string
*/
function randomString($len, $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
{
$len = (int)$len;
$charLength = UTF8::strlen($characters) - 1;
$string = '';
if ($len > 0) {
while ($len--) {
$string .= $characters[mt_rand(0, $charLength)];
}
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment