PHP Snippet 1:
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");
PHP Snippet 2:
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");
PHP Snippet 3:
function random_username($string) {
return vsprintf('%s%s%d', [...sscanf(strtolower($string), '%s %2s'), random_int(0, 100)]);
}
echo random_username("Mike Test"); // mikete25
PHP Snippet 4:
function random_username($string) {
sscanf(strtolower($string), '%s %2s', $first, $second);
return sprintf('%s%s%d', $first, $second, random_int(0, 100));
}
PHP Snippet 5:
function random_username($string) {
return preg_replace('/(\S+) (\S{2}).*/', '$1$2', strtolower($string)) . random_int(0, 100);
}
PHP Snippet 6:
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");
PHP Snippet 7:
$username = substr(str_replace(' ','',strtolower($name)), 0, 5).rand(1,3);
PHP Snippet 8:
<?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;
?>
PHP Snippet 9:
elonmusk74388
elon41960
elonmus77699
elonmus26248
elonmusk93154
elonmusk83467
elonm28690
elonmu79305
elonmu18227
elonmu28329
elon15113
elonmus38293
elonmusk77995
elonmusk77790
elonmu13349
elonmu73136
elonmus27741
elonmu72284
elon47465
elon85983
elonmus49720