You are here

function email_registration_cleanup_username in Email Registration 7

Same name and namespace in other branches
  1. 8 email_registration.module \email_registration_cleanup_username()

Function to clean up username.

e.g. Replace two or more spaces with a single underscore Strip illegal characters.

Parameters

string $name: The username to be cleaned up.

Return value

string Cleaned up username.

2 calls to email_registration_cleanup_username()
email_registration_user_insert in ./email_registration.module
Implements hook_user_insert().
hook_email_registration_name in ./email_registration.api.php
Implement this hook to generate a username for email_registration module.

File

./email_registration.module, line 103
Allows users to register with an e-mail address as their username.

Code

function email_registration_cleanup_username($name, $uid = NULL) {

  // 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('/\\s+/', '_', $name);

  // If there's nothing left use a default.
  $name = '' === $name ? t('user') : $name;
  if (!empty($uid)) {

    // Put uid on the end of the name.
    $name = $name . '_' . $uid;
  }

  // Truncate to a reasonable size.
  $name = drupal_strlen($name) > USERNAME_MAX_LENGTH - 10 ? drupal_substr($name, 0, USERNAME_MAX_LENGTH - 11) : $name;
  return $name;
}