合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
~~~ <?php /** * 构建各类有意义的随机数 * @since 2018-08-07 * @author zhaoxiang <zhaoxiang051405@gmail.com> */ namespace app\util; class StrRandom { /** * 构建一个随机浮点数 * @param int $min 整数部分的最小值,默认值为-999999999 * @param int $max 整数部分的最大值,默认值为999999999 * @param int $dmin 小数部分位数的最小值,默认值为 0 * @param int $dmax 小数部分位数的最大值,默认值为 8 * @return float * @author zhaoxiang <zhaoxiang051405@gmail.com> */ public static function randomFloat($min = -999999999, $max = 999999999, $dmin = 0, $dmax = 8) { $rand = ''; $intNum = mt_rand($min, $max); $floatLength = mt_rand($dmin, $dmax); if ($floatLength > 1) { $rand = Strs::randString($floatLength - 1, 1); } $floatEnd = mt_rand(1, 9); return floatval($intNum . '.' . $rand . $floatEnd); } /** * 随机字符串生成 * @param int $len 生成的字符串长度 * @return string */ public static function randomString($len = 6) { $chars = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]; $charsLen = count($chars) - 1; shuffle($chars); // 将数组打乱 $output = ""; for ($i = 0; $i < $len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } /** * 随机字数字字符串生成 * @param int $len 生成的数字字符串长度 * @return string */ public static function randomNumber($len = 4) { $chars = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ]; $charsLen = count($chars) - 1; shuffle($chars); // 将数组打乱 $output = ""; for ($i = 0; $i < $len; $i++) { $output .= $chars[mt_rand(0, $charsLen)]; } return $output; } /** * 获取随机的时间 * @param string $format PHP的时间日期格式化字符 * @return false|string * @author zhaoxiang <zhaoxiang051405@gmail.com> */ public static function randomDate($format = 'Y-m-d H:i:s') { $timestamp = time() - mt_rand(0, 86400 * 3650); return date($format, $timestamp); } /** * 随机生成一个邮箱地址 * @param string $domain 可以指定邮箱域名 * @return string * @author zhaoxiang <zhaoxiang051405@gmail.com> */ public static function randomEmail($domain = '') { $len = mt_rand(6, 16); $domain = $domain ? $domain : self::randomDomain(); return Strs::randString($len) . '@' . $domain; } /** * 随机生产手机号码 * @return string * @author zhaoxiang <zhaoxiang051405@gmail.com> */ public static function randomPhone() { $prefixArr = [133, 153, 173, 177, 180, 181, 189, 199, 134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 172, 178, 182, 183, 184, 187, 188, 198, 130, 131, 132, 155, 156, 166, 175, 176, 185, 186, 145, 147, 149, 170, 171]; shuffle($prefixArr); return $prefixArr[0] . Strs::randString(8, 1); } /** * 随机创建一个身份证号码 * @return string * @author zhaoxiang <zhaoxiang051405@gmail.com> */ public static function randomId() { $prefixArr = [ 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 71, 81, 82 ]; shuffle($prefixArr); $suffixArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'X']; shuffle($suffixArr); return $prefixArr[0] . '0000' . self::randomDate('Ymd') . Strs::randString(3, 1) . $suffixArr[0]; } } ~~~