A simple script to generate usernames based on a name and year of birth.
For more information see here: Programming Project Paperwork: Username generator on Google Docs.
You can use this on its own:
$usernamegenerator = new UsernameGenerator();
$username = $usernamegenerator->createNew($name, $dateofbirth);
The createNew
function takes those two arguments: $name
and $dateofbirth
. $name
should be a string containing a name, for example, "Samuel Elliott". $dateofbirth
can contain either a UNIX timestamp or any date string recognised by strtotime
.
You can also extend it. You could do this in a large or public application to check if a username already exists.
class MyUsernameGenerator extends UsernameGenerator {
public function isUnique($username) {
// Check if this username exists in the database
// ...
if(is_object($user_row))
return true;
else return false;
}
}
This lets the class know if a username already exists, and it can then re-generate the username.
On success, a username in the format: "SaElliott2001" will be returned. If the $name
argument contained multiple names (for example "Samuel Thomas Elliott") a string in the format "STElliott2001" will be used. If the username already exists "." and a number will be appended to the username (for example "SaElliott2011.2"). An Exception
object will be thrown on an error.