You are here

function _auto_username_clean_separators in Automatic User Names 7

Trims duplicate, leading, and trailing separators from a string.

Parameters

$string: The string to clean separators from.

$separator: The separator to use when cleaning.

Return value

The cleaned version of the string.

See also

auto_username_cleanstring()

1 call to _auto_username_clean_separators()
auto_username_cleanstring in ./auto_username.inc
Clean up a string segment to be used in a username.

File

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

Code

function _auto_username_clean_separators($string, $separator = NULL) {
  static $default_separator;
  if (!isset($separator)) {
    if (!isset($default_separator)) {
      $default_separator = variable_get('aun_separator', '-');
    }
    $separator = $default_separator;
  }
  $output = $string;

  // Clean duplicate or trailing separators.
  if (strlen($separator)) {

    // Escape the separator.
    $seppattern = preg_quote($separator, '/');

    // Trim any leading or trailing separators.
    $output = preg_replace("/^{$seppattern}+|{$seppattern}+\$/", '', $output);

    // Replace trailing separators around slashes.
    if ($separator !== '/') {
      $output = preg_replace("/{$seppattern}+\\/|\\/{$seppattern}+/", "/", $output);
    }

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