function uc_store_email_to_username in Ubercart 7.3
Same name and namespace in other branches
- 8.4 uc_store/uc_store.module \uc_store_email_to_username()
- 6.2 uc_store/uc_store.module \uc_store_email_to_username()
Derives a valid username from an e-mail address.
Parameters
$email: An e-mail address.
Return value
A username derived from the e-mail address, using the part of the address up to the @ with integers appended to the end if needed to avoid a duplicate username.
3 calls to uc_store_email_to_username()
- uc_cart_complete_sale_account in uc_cart/
uc_cart.module - Link a completed sale to a user.
- uc_order_create_form_create_submit in uc_order/
uc_order.admin.inc - Form submission handler for customer search.
- uc_order_select_customer in uc_order/
uc_order.admin.inc - Presents the customer search results and let one of them be chosen.
File
- uc_store/
uc_store.module, line 1715 - Contains global Ubercart functions and store administration functionality.
Code
function uc_store_email_to_username($email) {
// Default to the first part of the e-mail address.
$name = substr($email, 0, strpos($email, '@'));
// Remove possible illegal characters.
$name = preg_replace('/[^A-Za-z0-9_.-]/', '', $name);
// Trim that value for spaces and length.
$name = trim(substr($name, 0, USERNAME_MAX_LENGTH - 4));
// Make sure we don't hand out a duplicate username.
while (db_query("SELECT COUNT(uid) FROM {users} WHERE name LIKE :name", array(
':name' => $name,
))
->fetchField() > 0) {
// If the username got too long, trim it back down.
if (strlen($name) == USERNAME_MAX_LENGTH) {
$name = substr($name, 0, USERNAME_MAX_LENGTH - 4);
}
// Append a random integer to the name.
$name .= rand(0, 9);
}
return $name;
}