Generate Username from User First and Last Name – PHP
I recently needed to generate username based on first and last name of Facebook and Google users. So I created PHP functions that achieve the results I wanted. The function below simply combines the first and last name of the user, trims long names and adds a random number as suffix. So if I want to create a username from the name "Mike Tyson", the result will be "mikentyso94" etc.PHP
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
//generate a username from Full name
function generate_username($string_name="Mike Tyson", $rand_no = 200){
$username_parts = array_filter(explode(" ", strtolower($string_name))); //explode and lowercase name
$username_parts = array_slice($username_parts, 0, 2); //return only first two arry part
$part1 = (!empty($username_parts[0]))?substr($username_parts[0], 0,8):""; //cut first name to 8 letters
$part2 = (!empty($username_parts[1]))?substr($username_parts[1], 0,5):""; //cut second name to 5 letters
$part3 = ($rand_no)?rand(0, $rand_no):"";
$username = $part1. str_shuffle($part2). $part3; //str_shuffle to randomly shuffle all characters
return $username;
}
//usage
echo generate_username("Mike Tyson", 10);
Creating unique username with MySqli
We now know how to generate a username, but what if the username already exist in the database, it can result in duplicate usernames, which can cause unexpected outcomes and we certainly do not want that! So in next example, we will generate a username but this time we also check for existing username in the database for uniqueness.PHP
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
//Generate a unique username using Database
function generate_unique_username($string_name="Mike Tyson", $rand_no = 200){
while(true){
$username_parts = array_filter(explode(" ", strtolower($string_name))); //explode and lowercase name
$username_parts = array_slice($username_parts, 0, 2); //return only first two arry part
$part1 = (!empty($username_parts[0]))?substr($username_parts[0], 0,8):""; //cut first name to 8 letters
$part2 = (!empty($username_parts[1]))?substr($username_parts[1], 0,5):""; //cut second name to 5 letters
$part3 = ($rand_no)?rand(0, $rand_no):"";
$username = $part1. str_shuffle($part2). $part3; //str_shuffle to randomly shuffle all characters
$username_exist_in_db = username_exist_in_database($username); //check username in database
if(!$username_exist_in_db){
return $username;
}
}
}
function username_exist_in_database($username){
$mysqli = new mysqli('localhost','mysql_username','password','databasename'); //connect to database
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
$statement = $mysqli->prepare("SELECT id FROM users WHERE username =?");
$statement->bind_param('s', $username);
if($statement->execute()){
$statement->store_result();
return $statement->num_rows;
}
}
//usage
echo generate_unique_username("Mike Tyson", 10);