server/app/Utils/Str.php

43 lines
987 B
PHP

<?php
/**
* Author: cfn <cfn@leapy.cn>
*/
namespace App\Utils;
class Str
{
/**
* 获取UUID
* @param string $prefix
* @return string
*/
static function uuid(string $prefix = ''): string
{
$chars = md5(uniqid(mt_rand(), true));
$uuid = substr($chars, 0, 8) . '-';
$uuid .= substr($chars, 8, 4) . '-';
$uuid .= substr($chars, 12, 4) . '-';
$uuid .= substr($chars, 16, 4) . '-';
$uuid .= substr($chars, 20, 12);
return $prefix . $uuid;
}
/**
* 获取随机字符串
* @param int $num
* @param string $prefix
* @return string
*/
static function randStr(int $num, string $prefix = ""): string
{
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
$_str = "";
while ($num > 0) {
$_str .= substr($str, rand(0, strlen($str) - 1), 1);
$num--;
}
return $prefix . $_str;
}
}