Skip to content

Instantly share code, notes, and snippets.

@dilaouid
Created September 1, 2022 13:10
Show Gist options
  • Save dilaouid/f4bb26fa24dc66c5bc07b045d2009129 to your computer and use it in GitHub Desktop.
Save dilaouid/f4bb26fa24dc66c5bc07b045d2009129 to your computer and use it in GitHub Desktop.
Creating username with firstname and lastname (first letter of firstname + lastname)
<?php
function parse_login($start, $firstname, $lastname) {
$prefix = substr($firstname, 0, $start + 1);
$login = substr($prefix . $lastname, 0, 10);
return strtolower($login);
}
function define_login($mysql, $firstname, $lastname) {
$idx = 0;
$login = parse_login($idx, $firstname, $lastname);
$suffix = 1;
$prefixIdx = 1;
// countUserWithUsername is a function which count the user with this
// specific username. You have to do it yourself, according to the specificities
// of your tables
while (countUserWithUsername($mysql, $login) > 0) {
if ($idx >= strlen($firstname)) {
if (strlen($firstname) >= $prefixIdx - 1) {
$prefixIdx = 1;
$suffix = 1;
}
$login = parse_login($prefixIdx - 1, $firstname, $lastname) . $suffix;
$prefixIdx++;
} else
$login = parse_login($idx++, $firstname, $lastname);
}
return ($login);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment