根据全名php生成随机用户名

时间:2016-01-27 12:16:50

标签: php

我想以小写字母获取第一个名称,然后连接空格后面的前两个字符,最后用0到100之间的随机数连接。 因此,如果我的名字是it(": clicking button should open Dropbox Picker window", function(){ var element = $compile("<span data-dropbox extensions=\"extensions\"></span>")($scope); $scope.$digest(); var theButton = element.find('button'); spyOn($window, 'open'); theButton.triggerHandler('click'); expect($window.open).toHaveBeenCalled(); }); ,我希望输出为:"Mike Test"

mikete3

我的功能输出&#34; mike te84&#34;而且我不知道如何移除那个空间。

3 个答案:

答案 0 :(得分:5)

试试这个,总是使用trim来移除额外的空间。

function random_username($string) {
$pattern = " ";
$firstPart = strstr(strtolower($string), $pattern, true);
$secondPart = substr(strstr(strtolower($string), $pattern, false), 0,3);
$nrRand = rand(0, 100);

$username = trim($firstPart).trim($secondPart).trim($nrRand);
return $username;
}

echo random_username("Mike Test");

答案 1 :(得分:0)

尝试:

function random_username($string) {
    $name = split("[ .-]", $string);
    $firstname = $name[0];
    $lastname = $name[1];

    $firstname = strtolower($firstname);
    $lastname = strtolower(substr($lastname, 0, 2));
    $nrRand = rand(0, 100);
    return $firstname . $lastname . $nrRand;
}

echo random_username("Mike Test");

答案 2 :(得分:0)

更简单的方式

$username = substr(str_replace(' ','',strtolower($name)), 0, 5).rand(1,3);
相关问题