You are here

function auto_username_cleanstring in Automatic User Names 6

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

Clean up a string value to have only alphanumeric and separator values.

This function is largely stolen from pathauto.

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
Cleans tokens so they are PHP-friendly.

File

./auto_username.module, line 246

Code

function auto_username_cleanstring($string, $clean_slash = TRUE) {

  // Default words to ignore
  $ignore_words = array(
    "a",
    "an",
    "as",
    "at",
    "before",
    "but",
    "by",
    "for",
    "from",
    "is",
    "in",
    "into",
    "like",
    "of",
    "off",
    "on",
    "onto",
    "per",
    "since",
    "than",
    "the",
    "this",
    "that",
    "to",
    "up",
    "via",
    "with",
  );

  // Replace or drop punctuation based on user settings.
  $separator = variable_get('aun_separator', '-');
  $output = $string;
  $punctuation = auto_username_punctuation_chars();
  foreach ($punctuation as $name => $details) {
    $action = variable_get('aun_punctuation_' . $name, 0);

    // 2 is the action for "do nothing" with the punctuation
    if ($action != AUN_PUNCTUATION_DO_NOTHING) {

      // Slightly tricky inline if which either replaces with the separator or nothing
      $output = str_replace($details['value'], $action ? $separator : '', $output);
    }
  }

  // We definitely don't like slashes.
  if ($clean_slash) {
    $output = str_replace('/', '', $output);
  }

  // Reduce to the subset of ASCII96 letters and numbers
  if (variable_get('aun_reduce_ascii', FALSE)) {
    $pattern = '/[^a-zA-Z0-9\\/]+/ ';
    $output = preg_replace($pattern, $separator, $output);
  }

  /*
  // Get rid of words that are on the ignore list
  $ignore_re = "\b". preg_replace("/,/", "\b|\b", variable_get('pathauto_ignore_words', $ignore_words)) ."\b";

  if (function_exists('mb_ereg_replace')) {
    $output = mb_ereg_replace("/$ignore_re/i", "", $output);
  }
  else {
    $output = preg_replace("/$ignore_re/i", "", $output);
  }
  */

  // Replace whitespace with a separator.
  if (variable_get('aun_replace_whitespace', 0)) {
    $output = preg_replace("/\\s+/", $separator, $output);
  }

  // In preparation for pattern matching,
  // escape the separator if and only if it is not alphanumeric)
  if (isset($separator)) {
    if (preg_match('/^[^' . PREG_CLASS_ALNUM . ']+$/uD', $separator)) {
      $seppattern = $separator;
    }
    else {
      $seppattern = '\\' . $separator;
    }

    // Trim any leading or trailing separators (note the need to
    $output = preg_replace("/^{$seppattern}+|{$seppattern}+\$/", "", $output);

    // Replace multiple separators with a single one
    $output = preg_replace("/{$seppattern}+/", "{$separator}", $output);
  }

  // Enforce the maximum component length
  $maxlength = min(variable_get('pathauto_max_component_length', 100), 128);
  $output = drupal_substr($output, 0, $maxlength);
  return $output;
}