Created
July 18, 2023 05:54
-
-
Save zhanglianxin/d9c92b08f09eae68c003e8ef5a75c8d8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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('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