Question

I want to generate unique usernames based on some string (first 6 letters of domain names).

The problem is that the first 6 letters of domain names, may be the same.

Consider the following table:

Domain        | Username
--------------+------------
a.com         | acom
google.com    | google
yahoo.com     | yahooc
aaaaaa.com    | aaaaaa
aaaaa5.com    | aaaaa5 -- Because `aaaaa5` is still unique
aaaaaab.com   | aaaaa1 -- `aaaaaa` is taken, so we choose `aaaaa` + `1`
aaaaaac.com   | aaaaa2
aaaaaad.com   | aaaaa3
aaaaaae.com   | aaaaa4
aaaaaaf.com   | aaaaa6 -- We had `aaaaa5` before, so we go for `aaaaa6`
.
.
.
aaaaax.com    | aaaa10

I had really no idea where to begin, and didn't find anything after some search.


Update: Friends, I'm wondering because of the minuses!

It's nothing strange. Just gonna be like WHMCS's user generation system!

Was it helpful?

Solution

It's not clear how you want to generate the username but this is a way to get a unique one. Hope it gives you an idea where to start:

EDIT:

I edited the code so it generates unique usernames with a maximum of 6 characters. The for loop is just for demonstration purposes.

$basename = 'username';
$usernames = array();
for ($i = 0; $i < 25; $i++) {   
    $username = substr($basename,0,6);
    $number = 0;
    while(in_array($username, $usernames)) {
        $length = strlen($username)-strlen($number);
        $username = substr($basename,0,$length).$number;
        $number++;
    }
    $usernames[] = $username;
    echo $username.'<br>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top