插件功能概述
多种生成策略
智能随机选择
安全回退机制
代码实现细节
<?php
class UsernameGenerator {
private const STRATEGIES = [
'纯中文' => ['weight' => 35, 'components' => ['chinese' => true]],
'纯英文' => ['weight' => 15, 'components' => ['english' => true]],
'纯数字' => ['weight' => 20, 'components' => ['numeric' => true]],
'中英' => ['weight' => 10, 'components' => ['chinese' => true, 'english' => true]],
'中数' => ['weight' => 10, 'components' => ['chinese' => true, 'numeric' => true]],
'英数' => ['weight' => 5, 'components' => ['english' => true, 'numeric' => true]],
'全混合' => ['weight' => 5, 'components' => ['chinese' => true, 'english' => true, 'numeric' => true]]
];
public static function generate(): string {
$strategy = self::selectStrategy();
$components = self::buildComponents($strategy);
shuffle($components);
$username = implode('', $components);
// 确保用户名不为空,添加安全回退
if (empty($username)) {
$username = 'user_' . wp_rand(10000, 99999);
}
return $username;
}
private static function selectStrategy(): array {
$totalWeight = array_sum(array_column(self::STRATEGIES, 'weight'));
$random = rand(1, $totalWeight);
$current = 0;
foreach (self::STRATEGIES as $name => $strategy) {
$current += $strategy['weight'];
if ($random <= $current) {
return $strategy['components'];
}
}
return current(self::STRATEGIES)['components'];
}
private static function buildComponents(array $strategy): array {
return array_filter([
isset($strategy['chinese']) ? self::chineseComponent($strategy['chinese'] ?? false) : null,
isset($strategy['english']) ? self::englishComponent($strategy['english'] ?? false) : null,
isset($strategy['numeric']) ? self::numericComponent($strategy['numeric'] ?? false) : null
]);
}
private static function chineseComponent(bool $extended = false): string {
// 从数据库获取基础关键词配置
$baseWordsStr = get_option('bulk_user_base_words', "宝哥
安静
美美
乐天
达人
粉丝
朋友");
$baseWords = array_filter(array_map('trim', explode("\n", $baseWordsStr)));
$baseWords = array_values($baseWords); // 重新索引数组
if (!$extended || rand(0,1)) {
return $baseWords[array_rand($baseWords)];
}
// 从数据库获取形容词和名词配置
$adjectivesStr = get_option('bulk_user_adjectives', "快乐
阳光
超级
无敌
神秘
聪明");
$adjectives = array_filter(array_map('trim', explode("\n", $adjectivesStr)));
$adjectives = array_values($adjectives);
$nounsStr = get_option('bulk_user_nouns', "宝贝
达人
会员
玩家
伙伴
天使");
$nouns = array_filter(array_map('trim', explode("\n", $nounsStr)));
$nouns = array_values($nouns);
return $adjectives[array_rand($adjectives)] . $nouns[array_rand($nouns)];
}
private static function englishComponent(bool $standalone = false): string {
if($standalone) {
$words = ['sunshine','moonlight','starlight','blueSky','happyDay'];
return $words[array_rand($words)];
}
$prefixes = ['user','mem','pro','game','fan'];
$suffix = substr(str_shuffle('abcdefghjkmnpqrstuvwxyz'), 0, rand(4,6));
return $prefixes[array_rand($prefixes)].$suffix;
}
private static function numericComponent(bool $long = false): string {
$length = $long ? rand(6,8) : rand(4,6);
return (string)rand(pow(10, $length-1), pow(10, $length)-1);
}
}
使用场景
总结
© 版权声明
温馨提示:本站提供的一切软件、教程和内容信息都来自网络收集整理,仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,版权争议与本站无关。用户必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
THE END
喜欢就支持一下吧
相关推荐
暂无评论内容