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
source share