How to create a username from an email address - PHP

I have a registration page on my website, where the user must specify only the email address and password.

I want you to be able to automatically create a username for this user using the first part of the provided message;

Custom supplies gordon @ yourdomain.com, I want to make the username 'gordon'

I do not need to explain how to create a form or provide data to the database, just a code to extract data from the letter you provided and, if necessary, if duplication occurs, add the number at the end.

Hope this makes sense, seems like a core feature, but can't find it anywhere on the net!

+4
source share
9 answers

This is not a good idea, just use their full email address. The following email addresses may be different people, but they will become the same on your system.

samename@gmail.com samename@yahoo.com 

Adding a number to the end will make the user remember something unique to your system and cause a lot of confusion at their end.

+13
source

Agreed, you are depriving a necessarily unique identifier in a unique code. If you do not want to add some processing to add a number to the username or something else. If this is what you really want to do, this should set $ username for the material to the email address:

 <?php $username = preg_replace('/([^@]*).*/', '$1', $email); ?> 
+5
source

Sort of:

 $username = left($email, stripos($email, '@')); 

must do. You may want to learn regular expressions for these tasks.

Then you add a counter:

 function countOccurrences($name) { $con = mysql_connect(___, ___, ___); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db(___, $con); $result = mysql_query(" SELECT COUNT(*) AS countOccurrences FROM users WHERE username LIKE '" . mysql_real_escape_string($name, $con) . "%' "); $row = mysql_fetch_array($result); $number = $row['countOccurrences']; mysql_close($con); return $number; } 

and then:

 $countUsers = countOccurrences($username); if ($countUsers>0) { $username = $username . $countUsers; } 

IMPORTANT: Consider using all email as a username: you do not want gordon@flash.net be considered equal to gordon@clash.com

NOTE: the sample code counts gordon, gordon1, gordon2, but gordonbah, gordonq, gordonxxx too

NOTE: this is pretty crude and should not be considered PHP best practice; it is just to give a general idea

+2
source
 $username = gordon@example.com ; $username_arr = explode('@',$username); $username = $username_arr[0]; if( !is_taken( $username ) ) { //The name is not taken. } else { //The name is taken, add numbers and do the search again. } function is_taken( $username ) { $db_link = mysql_connect($host,$user,$pass) or die('Could not connect to database'); $username = mysql_real_escape_string( $username ); $sql = "SELECT * FROM `database`.`users` WHERE `username` = '$username'"; $res = mysql_query( $sql , $db_link ); return mysql_num_rows( $res ) == 1; } 
+2
source
 <?php preg_match('/[^@]+)@/',$email,$matches); $username = $matches[1]; check_availability($username); ?> 
+1
source

Starting with PHP 5.3.0 you can also use:

 $email = ' test@stackoverflow.com '; $user = strstr($email, '@', true); 

This will return the entire line from the beginning to the first appearance of "@". What in this case:

 test 
+1
source

Meets your needs: D

 $username = substr($username, 0, strpos($username, '@')); $username = mysql_real_escape_string($username); $result = mysql_query('SELECT `username` FROM `users` WHERE `username` = \'' . $username . '%\';'); if (mysql_num_rows($result) > 0) { $i = 0; while ($name_arr = mysql_fetch_assoc($result)) { $name = $name_arr['username']; $after = substr($name, strlen($username)); if (ctype_digit($after)) { if (($after = (int) $after) > $i) { $i = $after; } } } if ($i > 0) { $username .= $i; } } 
0
source

I use something like this,

 $cust_email = " test@gmail.com "; $parts = explode("@", $cust_email); $cust_name = $parts[0]; 

I use to set the default username if the user does not specify his name in the profile.

0
source

You can use the strstr function to find a specific word from a string

  <?php $username = strstr(" username@domain.com ",'@',true); //get text before @ /* echo strstr(" username@domain.com ","@"); // get text after @ */ check_availability($username); ?> 
0
source

Source: https://habr.com/ru/post/1285812/


All Articles