123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- /**
- * Str.php
- * Created on 2020/9/7 15:23
- * Create by Wpeishi
- */
- namespace App\Handlers;
- class Str
- {
- /**
- * 获取随机字符串
- * @param int $length 长度
- * @param string $type 类型
- * @param int $convert 转换大小写
- * @return string 随机字符串
- */
- public static function random($length = 6, $type = 'string', $convert = 0)
- {
- $config = array(
- 'number' => '1234567890',
- 'letter' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
- 'string' => 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789',
- 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
- );
- if (!isset($config[$type]))
- $type = 'string';
- $string = $config[$type];
- $code = '';
- $strlen = strlen($string) - 1;
- for ($i = 0; $i < $length; $i++) {
- $code .= $string{mt_rand(0, $strlen)};
- }
- if (!empty($convert)) {
- $code = ($convert > 0) ? strtoupper($code) : strtolower($code);
- }
- return $code;
- }
- /**
- * 生成唯一不重复 32 位字符串
- */
- public static function uniqueString()
- {
- return md5(uniqid(microtime(true), true));
- }
- /**
- * 返回两位数字母
- * @param $in_array
- * @return string
- */
- public static function getChr($in_array)
- {
- $return_sta = chr(rand(65, 90)) . chr(rand(65, 90));
- if (!count($in_array) || !in_array($return_sta, $in_array)) {
- return $return_sta;
- }
- $str = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'L', 'M', 'N'];
- for ($i = 0; $i < strlen((string)$str); $i++) {
- for ($j = 0; $j < strlen((string)$str); $j++) {
- if (!in_array($str[$i] . $str[$j], $in_array)) {
- return $str[$i] . $str[$j];
- continue;
- }
- }
- }
- }
- }
|