public static function AutoUsernameSettingsForm::autoUsernameCleanSeparators in Automatic User Names 8
Trims duplicate, leading, and trailing separators from a string.
Parameters
string $string: The string to clean separators from.
string $separator: The separator to use when cleaning.
Return value
mixed The cleaned version of the string.
See also
autoUsernameCleanSeparators()
1 call to AutoUsernameSettingsForm::autoUsernameCleanSeparators()
- AutoUsernameSettingsForm::autoUsernameCleanstring in src/
Form/ AutoUsernameSettingsForm.php - Clean up a string segment to be used in a username.
File
- src/
Form/ AutoUsernameSettingsForm.php, line 623
Class
- AutoUsernameSettingsForm
- Class AutoUsernameSettingsForm.
Namespace
Drupal\auto_username\FormCode
public static function autoUsernameCleanSeparators($string, $separator = NULL) {
static $default_separator;
if (!isset($separator)) {
if (!isset($default_separator)) {
$default_separator = \Drupal::state()
->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;
}