Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于全名php生成随机用户名_Php - Fatal编程技术网

基于全名php生成随机用户名

基于全名php生成随机用户名,php,Php,我想获取小写的名字,然后连接空格后的前两个字符,最后用0到100的随机数连接。 因此,如果我的名字是“Mike Test”我希望输出是:mikete3 function random_username($string) { $pattern = " "; $firstPart = strstr(strtolower($string), $pattern, true); $secondPart = substr(strstr(strtolower($string), $pa

我想获取小写的名字,然后连接空格后的前两个字符,最后用0到100的随机数连接。 因此,如果我的名字是
“Mike Test”
我希望输出是:
mikete3

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 = $firstPart.$secondPart.$nrRand;
    return $username;
}

echo random_username("Mike Test");

我的函数输出“mike te84”,我不知道如何删除该空间。

试试这个,总是使用修剪来删除额外的空间

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");
尝试:

简单的方法

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

有一种较短的方法可以做到这一点,只需调用
trim()
$username = substr(str_replace(' ','',strtolower($name)), 0, 5).rand(1,3);