You are here

function auto_username_cleanstring in Automatic User Names 7

Same name and namespace in other branches
  1. 5 auto_username.module \auto_username_cleanstring()
  2. 6 auto_username.module \auto_username_cleanstring()

Clean up a string segment to be used in a username.

Performs the following possible alterations:

  • Remove all HTML tags.
  • Process the string through the transliteration module.
  • Replace or remove punctuation with the separator character.
  • Remove back-slashes.
  • Replace non-ascii and non-numeric characters with the separator.
  • Remove common words.
  • Replace whitespace with the separator character.
  • Trim duplicate, leading, and trailing separators.
  • Convert to lower-case.
  • Shorten to a desired length and logical position based on word boundaries.

Parameters

$string: A string to clean.

Return value

The cleaned string.

1 call to auto_username_cleanstring()
auto_username_clean_token_values in ./auto_username.module
Clean token values.

File

./auto_username.inc, line 60
Miscellaneous functions for auto_username.

Code

function auto_username_cleanstring($string) {

  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['cache'] =& drupal_static(__FUNCTION__);
  }
  $cache =& $drupal_static_fast['cache'];

  // Generate and cache variables used in this function so that on the second
  // call to auto_username_cleanstring() we focus on processing.
  if (!isset($cache)) {
    $cache = array(
      'separator' => variable_get('aun_separator', '-'),
      'transliterate' => variable_get('aun_transliterate', FALSE) && module_exists('transliteration'),
      'punctuation' => array(),
      'reduce_ascii' => (bool) variable_get('aun_reduce_ascii', FALSE),
      'ignore_words_regex' => FALSE,
      'replace_whitespace' => (bool) variable_get('aun_replace_whitespace', FALSE),
      'lowercase' => (bool) variable_get('aun_case', AUN_CASE_LOWER),
      'maxlength' => min(variable_get('aun_max_component_length', 60), _auto_username_get_schema_name_maxlength()),
    );

    // Generate and cache the punctuation replacements for strtr().
    $punctuation = auto_username_punctuation_chars();
    foreach ($punctuation as $name => $details) {
      $action = variable_get('aun_punctuation_' . $name, AUN_PUNCTUATION_REMOVE);
      switch ($action) {
        case AUN_PUNCTUATION_REMOVE:
          $cache['punctuation'][$details['value']] = '';
          break;
        case AUN_PUNCTUATION_REPLACE:
          $cache['punctuation'][$details['value']] = $cache['separator'];
          break;
        case AUN_PUNCTUATION_DO_NOTHING:

          // Literally do nothing.
          break;
      }
    }

    // Generate and cache the ignored words regular expression.
    $ignore_words = variable_get('aun_ignore_words', '');
    $ignore_words_regex = preg_replace(array(
      '/^[,\\s]+|[,\\s]+$/',
      '/[,\\s]+/',
    ), array(
      '',
      '\\b|\\b',
    ), $ignore_words);
    if ($ignore_words_regex) {
      $cache['ignore_words_regex'] = '\\b' . $ignore_words_regex . '\\b';
      if (function_exists('mb_eregi_replace')) {
        $cache['ignore_words_callback'] = 'mb_eregi_replace';
      }
      else {
        $cache['ignore_words_callback'] = 'preg_replace';
        $cache['ignore_words_regex'] = '/' . $cache['ignore_words_regex'] . '/i';
      }
    }
  }

  // Empty strings do not need any proccessing.
  if ($string === '' || $string === NULL) {
    return '';
  }

  // Remove all HTML tags from the string.
  $output = strip_tags(decode_entities($string));

  // Optionally transliterate (by running through the Transliteration module)
  if ($cache['transliterate']) {
    $output = transliteration_get($output);
  }

  // Replace or drop punctuation based on user settings
  $output = strtr($output, $cache['punctuation']);

  // Reduce strings to letters and numbers
  if ($cache['reduce_ascii']) {
    $output = preg_replace('/[^a-zA-Z0-9\\/]+/', $cache['separator'], $output);
  }

  // Get rid of words that are on the ignore list
  if ($cache['ignore_words_regex']) {
    $words_removed = $cache['ignore_words_callback']($cache['ignore_words_regex'], '', $output);
    if (drupal_strlen(trim($words_removed)) > 0) {
      $output = $words_removed;
    }
  }

  // Replace whitespace with the separator.
  if ($cache['replace_whitespace']) {
    $output = preg_replace('/\\s+/', $cache['separator'], $output);
  }

  // Trim duplicates and remove trailing and leading separators.
  $output = _auto_username_clean_separators($output, $cache['separator']);

  // Optionally convert to lower case.
  if ($cache['lowercase']) {
    $output = drupal_strtolower($output);
  }

  // Shorten to a logical place based on word boundaries.
  $output = truncate_utf8($output, $cache['maxlength'], TRUE);
  return $output;
}