Skip to content

Instantly share code, notes, and snippets.

@zhanglianxin
Created July 18, 2023 05:54
Show Gist options
  • Save zhanglianxin/d9c92b08f09eae68c003e8ef5a75c8d8 to your computer and use it in GitHub Desktop.
Save zhanglianxin/d9c92b08f09eae68c003e8ef5a75c8d8 to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('random_str')) {
/**
* Generate specific length random string
*
* @param $len
* @param $special
* @return string
*/
function random_str($len = 4, $special = false)
{
$tuple = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
if ($special) {
$tuple .= '!@#$%^&*()';
}
$str = '';
$tupleLen = mb_strlen($tuple) - 1;
for ($i = 0; $i < $len; $i++) {
$str .= $tuple[mt_rand(0, $tupleLen)];
}
return $str;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment