From c85ca31718cd9941d623c02268cb91164408cebe Mon Sep 17 00:00:00 2001 From: xboard Date: Fri, 8 Dec 2023 20:16:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E6=94=AF=E6=8C=81[*-*]=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Utils/Helper.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/Utils/Helper.php b/app/Utils/Helper.php index 028d0b0..3b3921f 100644 --- a/app/Utils/Helper.php +++ b/app/Utils/Helper.php @@ -111,7 +111,8 @@ class Helper public static function getSubscribeUrl($path) { $subscribeUrls = explode(',', admin_setting('subscribe_url')); - $subscribeUrl = $subscribeUrls[rand(0, count($subscribeUrls) - 1)]; + $subscribeUrl = $subscribeUrls[array_rand($subscribeUrls)]; + $subscribeUrl = self::replaceRandomNumber($subscribeUrl); if ($subscribeUrl) return $subscribeUrl . $path; return url($path); } @@ -126,4 +127,27 @@ class Helper $encoded = base64_encode($data); return str_replace(['+', '/', '='], ['-', '_', ''], $encoded); } + + /** + * 替换字符串中的 [num1-num2] 格式为介于 num1 和 num2 之间的随机数字 + * + * @param string $input 用户输入的字符串 + * @return string 替换后的字符串 + */ + public static function replaceRandomNumber($input) { + // 匹配 [1-4999] 格式的正则表达式 + $pattern = '/\[(\d+)-(\d+)\]/'; + + // 使用 preg_replace_callback 替换匹配到的内容 + $result = preg_replace_callback($pattern, function ($matches) { + // 提取最小和最大值 + $min = intval($matches[1]); + $max = intval($matches[2]); + // 生成随机数 + $randomNumber = rand($min, $max); + return $randomNumber; + }, $input); + + return $result; + } }