随机字符串是PHP开发中经常需要的功能, 具体实现代码如下:
/** * 生成指定位数的随机字符串 * * @param int $num 位数 * @param bool $isLower 英文是否小写,默认小写 * @return string * @author lichunguang 153102250@qq.com * @since 2022/10/8 下午1:30 */ function getRandomString(int $num, bool $isLower=true){ $newFileName = ''; $chars = "1234567890qwertyuiopasdfghjklzxcvbnm";//随机生成图片名 if($isLower === false){ $chars .= strtoupper('qwertyuiopasdfghjklzxcvbnm'); } //echo $chars;exit; $totalLen = strlen($chars); for ($i = 0; $i < $num; $i++) { $newFileName .= $chars[mt_rand(0, $totalLen - 1)]; } return $newFileName; }