You are here

function email_registration_cleanup_username in Email Registration 8

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

Cleans up username.

Run username sanitation, 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.

3 calls to email_registration_cleanup_username()
EmailRegistrationUserName::transform in src/Plugin/migrate/process/EmailRegistrationUserName.php
Performs the associated process.
email_registration_user_insert in ./email_registration.module
Implements hook_ENTITY_TYPE_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 email address as their username.

Code

function email_registration_cleanup_username($name) {

  // 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;

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