SEO只是外表
技术才是我的追求

PHP生成随机英文用户名

最近用wordpress来做英文的博客,需要虚构一些随机的英文用户名来冒充访客发表评论,因为对PHP不熟悉(压根没学过),只能在百度和google查找方法。办法有3个:

1、随机字母数字组合法

这个方法的思路:26个字母大小写 +10个数字(0~9) 随机组合成6到10个的字符串最为用户名,代码如下:

function random_user($len = 8)
{
    $user = '';
    $lchar = 0;
    $char = 0;
    for($i = 0; $i < $len; $i++)
    {
        while($char == $lchar)
       {
            $char = rand(48, 109);
            if($char > 57) $char += 7;
            if($char > 90) $char += 6;
        }
       $user .= chr($char);
       $lchar = $char;
    }
return $user;
}

调用方法(默认生成8个字符串):

<?php echo random_user();?>

生产的英文用户名如下:

4c3yXssf,6IG0bdfa,u5NDh5d3,spBhdgPn,49otf8ub,NE8zg2dS

缺点很明显:不靠谱,可读性差,一看就知道是代码生成的。

2、调用现成的API

这个思路也很简单,直接调用可以生成用户数据的API,当然前提是要有这种API才行。我google一下,还真给我找着了:

https://randomuser.me/api/

以json格式直接返回一个用户的各种信息,包括:姓名,性格,年龄,城市,邮箱,头像等等…. 。下面是返回的数据:

{"results":[
    {
     "gender":"female",
     "name":{
        "title":"miss",
        "first":"frida",
        "last":"christiansen"
      },
     "location":{
        "street":"2087 oddenvej",
        "city":"ulsted, hals",
        "state":"danmark",
        "postcode":33076
      },
     "email":"frida.christiansen@example.com",
     "login":{
        "username":"brownostrich869",
        "password":"blacks",
        "salt":"hnroL361",
        "md5":"e77b5d194fa24307d94166fdb073489a",
        "sha1":"1f7c5119845d31071035ba786cfd1829ca4188de",
        "sha256":"37e202da9ee700e3b21a1260de0f2697f2a75357cacd6124e73eee39b21c0f05"
     },
     "dob":"1977-04-27 18:44:16",
     "registered":"2014-12-11 11:11:29",
     "phone":"97377508",
     "cell":"04206356",
     "id":{
        "name":"CPR",
        "value":"018563-3343"
     },
     "picture":{
        "large":"https://randomuser.me/api/portraits/women/58.jpg",
        "medium":"https://randomuser.me/api/portraits/med/women/58.jpg",
        "thumbnail":"https://randomuser.me/api/portraits/thumb/women/58.jpg"
     },
    "nat":"DK"
    }
  ],
  "info":{
    "seed":"9c5a7af03b97edcf",
    "results":1,
    "page":1,
    "version":"1.1"
  }
}

使用说明文档:

https://randomuser.me/documentation

如果使用python调用和处理这个API是没有问题的,但是对于php我就毫无头绪了,只能放弃这个方法。

思路可行,能力有限,日后再研

3、字典随机组合法

方法思路:找出外国人最常用的姓(Most Common Surnames),最常用的名(Most Common Male Names,Most Common Female Names),再随机组合成一个常用的英文用户名。

function random_user()
{
$male_names = array("James","John","Robert","Michael","William","David","Richard","Charles","Joseph","Thomas","Christopher","Daniel","Paul","Mark","Donald","George","Kenneth","Steven","Edward");
$famale_names = array("Mary","Patricia","Linda","Barbara","Elizabeth","Jennifer","Maria","Susan","Margaret","Dorothy","Lisa","Nancy","Karen","Betty","Helen","Sandra","Donna","Carol","Ruth");
$surnames = array("Smith","Jones","Taylor","Williams","Brown","Davies","Evans","Wilson","Thomas","Roberts","Johnson","Lewis","Walker","Robinson","Wood","Thompson","White","Watson","Jackson");
$frist_num = mt_rand(0,19);
$sur_num = mt_rand(0,19);
$type = rand(0,1);
if($type==0){
    $username=$male_names[$frist_num]." ".$surnames[$sur_num];
} else {
    $username=$famale_names[$frist_num]." ".$surnames[$sur_num];
}
return $username;
}

这里使用了20个常用男性名,20常用个女性名,20个常用的姓来组合用户名,如果需要更多的数据可以看看这里:外国人最常用的100个姓,最常用的100个名

调用的方法和上面是一样的,生成的英文用户名如下:

Mary Williams,Daniel Johnson,John Jones,Maria Evans,Donna Smith,Barbara Smith

可以看出这个方法生成的英文用户名很靠谱,可读性高,而且可生成数量众多(20*20+20*20 共800个)。最后我也是使用了这个函数。

这个三个方法的思路同样可用于生成中文用户名。

未经允许不得转载:棋哥SEO博客 » PHP生成随机英文用户名

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址