'Generate random username based on full name php
I want to grab the first name in lowercase then concatenate the first two characters after the space and finally concatenate that with a random number from 0 to 100.
So if my name is "Mike Test"
I want the output to be: 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");
My function outputs "mike te84" and I don't know how to remove that space.
Solution 1:[1]
Try this, always use trim to remove extra space.
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");
Solution 2:[2]
Regex solution
function random_username($string) {
$nrRand = rand(0, 100);
$string = strtolower($string);
return preg_replace('/^(.+)\s(.{2}).+$/', '$1$2', $string, 1) . $nrRand;
}
echo random_username("Mike Test");
Solution 3:[3]
- Convert all letters to lowercase with
strtolower()
. - Isolate the first word and the first two characters after the space.
- Create and return a space-less string from the two isolated values and the randomly generated integer.
Code: (Demo) (leveraging Spread Operator in Array Expression from PHP7.4)
function random_username($string) {
return vsprintf('%s%s%d', [...sscanf(strtolower($string), '%s %2s'), random_int(0, 100)]);
}
echo random_username("Mike Test"); // mikete25
An alternative syntax can declare variables to make it more intuitive/expressive. (Demo)
function random_username($string) {
sscanf(strtolower($string), '%s %2s', $first, $second);
return sprintf('%s%s%d', $first, $second, random_int(0, 100));
}
Or apply the same behavior with preg_replace()
where \S
means any non-whitespace character: (Demo)
function random_username($string) {
return preg_replace('/(\S+) (\S{2}).*/', '$1$2', strtolower($string)) . random_int(0, 100);
}
Solution 4:[4]
Try:
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");
Solution 5:[5]
Simpler way
$username = substr(str_replace(' ','',strtolower($name)), 0, 5).rand(1,3);
Solution 6:[6]
You can use this code to generate a random username based on the user's full name.
<?php
function generateUsername($fullName)
{
$removedMultispace = preg_replace('/\s+/', ' ', $fullName);
$sanitized = preg_replace('/[^A-Za-z0-9\ ]/', '', $removedMultispace);
$lowercased = strtolower($sanitized);
$splitted = explode(" ", $lowercased);
if (count($splitted) == 1) {
$username = substr($splitted[0], 0, rand(3, 6)) . rand(111111, 999999);
} else {
$username = $splitted[0] . substr($splitted[1], 0, rand(0, 4)) . rand(11111, 99999);
}
return $username;
}
$username = generateUsername("Elon Musk");
echo $username;
?>
Output:
elonmusk74388
elon41960
elonmus77699
elonmus26248
elonmusk93154
elonmusk83467
elonm28690
elonmu79305
elonmu18227
elonmu28329
elon15113
elonmus38293
elonmusk77995
elonmusk77790
elonmu13349
elonmu73136
elonmus27741
elonmu72284
elon47465
elon85983
elonmus49720
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kiritkumar |
Solution 2 | |
Solution 3 | |
Solution 4 | Andre Zimpel |
Solution 5 | Elyor |
Solution 6 |