function email_registration_unique_username in Email Registration 6
Same name and namespace in other branches
- 8 email_registration.module \email_registration_unique_username()
- 7 email_registration.module \email_registration_unique_username()
Given a starting point for a Drupal username (e.g. the name portion of an email address) return a legal, unique Drupal username. This function is designed to work on the results of the /user/register or /admin/people/create forms which have already called user_validate_name, valid_email_address or a similar function. If your custom code is creating users, you should ensure that the email/name is already validated using something like that.
Parameters
$name: A name from which to base the final user name. May contain illegal characters; these will be stripped.
$uid: (optional) Uid to ignore when searching for unique user (e.g. if we update the username after the {users} row is inserted)
Return value
A unique user name based on $name.
See also
1 call to email_registration_unique_username()
- email_registration_user in ./
email_registration.module - Implementation of hook_user().
File
- ./
email_registration.module, line 71 - For registration process without a username
Code
function email_registration_unique_username($name, $uid = 0) {
// Strip illegal characters.
$name = preg_replace('/[^\\x{80}-\\x{F7} a-zA-Z0-9@_.\'-]/', '', $name);
// Strip leading and trailing spaces.
$name = trim($name);
// Convert any other series of spaces to a single underscore.
$name = preg_replace('/ +/', '_', $name);
// If there's nothing left use a default.
$name = '' === $name ? t('user') : $name;
// Truncate to reasonable size.
$name = drupal_strlen($name) > USERNAME_MAX_LENGTH - 10 ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;
// Iterate until we find a unique name.
$i = 0;
do {
$new_name = empty($i) ? $name : $name . '_' . $i;
$found = db_result(db_query_range("SELECT uid from {users} WHERE uid <> %d AND name = '%s'", $uid, $new_name, 0, 1));
$i++;
} while (!empty($found));
return $new_name;
}