You are here

function uc_store_email_to_username in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_store/uc_store.module \uc_store_email_to_username()
  2. 7.3 uc_store/uc_store.module \uc_store_email_to_username()

Derives a valid username from an e-mail address.

Parameters

string $email: An e-mail address.

Return value

string 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()
CartManager::completeSaleAccount in uc_cart/src/CartManager.php
Link a completed sale to a user.
OrderAdminController::selectCustomer in uc_order/src/Controller/OrderAdminController.php
Presents the customer search results and let one of them be chosen.
OrderCreateForm::submitForm in uc_order/src/Form/OrderCreateForm.php
Form submission handler.

File

uc_store/uc_store.module, line 684
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 = mb_substr($email, 0, mb_strpos($email, '@'));

  // Remove possible illegal characters.
  $name = preg_replace('/[^A-Za-z0-9_.-]/', '', $name);

  // Trim that value for spaces and length.
  $name = trim(mb_substr($name, 0, UserInterface::USERNAME_MAX_LENGTH - 4));

  // Make sure we don't hand out a duplicate username.
  while (user_load_by_name($name)) {

    // If the username got too long, trim it back down.
    if (mb_strlen($name) == UserInterface::USERNAME_MAX_LENGTH) {
      $name = mb_substr($name, 0, UserInterface::USERNAME_MAX_LENGTH - 4);
    }

    // Append a random integer to the name.
    $name .= rand(0, 9);
  }
  return $name;
}