douhuang9886 2013-07-04 06:43
浏览 29
已采纳

PHP中的用户名建议

I want to create a function in php to suggest usernames, if the entered username is not available. For example, username entered is 'bingo' and it not available, then the system should suggest a list of usernames like this

bi_n_go
go-nbi
b-n_gio
b-ng-oi
...

Rules for creating username are :

  • minimum length 6
  • username can contain maximum 3 symbols(only hyphens, underscores)
  • username must start with alphanumeric and end with alphanumeric

Any help and suggestions will be highly appreciable. Thanks.

  • 写回答

3条回答 默认 最新

  • dourunlao1642 2013-07-04 07:03
    关注

    Try following code:

    <?php
    //mutates given user name, producing possibly incorrect username
    function mutate($uname)
    {
        $x = str_split($uname);
    
        //sort with custom function, that tries to produce only slightly
        //random user name (as opposed to completely shuffling)
        uksort($x, function($a, $b)
        {
            $chance = mt_rand(0, 3);
            if ($chance == 0)
            {
                return $b - $a;
            }
            return $a - $b;
        });
    
        //insert randomly dashes and underscores
        //(multiplication for getting more often 0 than 3)
        $chance = mt_rand(0, 3) * mt_rand(0, 3) / 3.;
        for ($i = 0; $i < $chance; $i ++)
        {
            $symbol = mt_rand() & 1 ? '-' : '_';
            $pos = mt_rand(0, count($x));
            array_splice($x, $pos, 0, $symbol);
        }
        return join('', $x);
    }
    
    //validates the output so you can check whether new user name is correct
    function validate($uname)
    {
        //does not start nor end with alphanumeric characters
        if (!preg_match('/^[a-zA-Z0-9].*[a-zA-Z0-9]$/', $uname))
        {
            return false;
        }
        //does contain more than 3 symbols
        $noSymbols = preg_replace('/[^a-zA-Z0-9]+/', '', $uname);
        if (strlen($uname) - strlen($noSymbols) > 3)
        {
            return false;
        }
        //shorter than 6 characters
        if (strlen($uname) < 6)
        {
            return false;
        }
        return true;
    }
    

    Example usage:

    $uname = 'bingo';
    $desired_num = 5;
    $sug = [];
    while (count($sug) < $desired_num)
    {
        $mut = mutate($uname);
        if (!validate($mut))
        {
            continue;
        }
        if (!in_array($mut, $sug) and $mut != $uname)
        {
            $sug []= $mut;
        }
    }
    print_r($sug);
    

    Example output:

    Array
    (
        [0] => i-g-obn
        [1] => bi-gno
        [2] => bi_ngo
        [3] => i-bnog
        [4] => bign-o
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私