You are here

public function AliasCleaner::getCleanSeparators in Pathauto 8

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

Parameters

string $string: The string to clean path separators from.

string $separator: The path separator to use when cleaning.

Return value

string The cleaned version of the string.

Overrides AliasCleanerInterface::getCleanSeparators

See also

pathauto_cleanstring()

pathauto_clean_alias()

2 calls to AliasCleaner::getCleanSeparators()
AliasCleaner::cleanAlias in src/AliasCleaner.php
Clean up an URL alias.
AliasCleaner::cleanString in src/AliasCleaner.php
Clean up a string segment to be used in an URL alias.

File

src/AliasCleaner.php, line 125

Class

AliasCleaner
Provides an alias cleaner.

Namespace

Drupal\pathauto

Code

public function getCleanSeparators($string, $separator = NULL) {
  $config = $this->configFactory
    ->get('pathauto.settings');
  if (!isset($separator)) {
    $separator = $config
      ->get('separator');
  }
  $output = $string;
  if (strlen($separator)) {

    // Trim any leading or trailing separators.
    $output = trim($output, $separator);

    // Escape the separator for use in regular expressions.
    $seppattern = preg_quote($separator, '/');

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

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

      // If the separator is a slash, we need to re-add the leading slash
      // dropped by the trim function.
      $output = '/' . $output;
    }
  }
  return $output;
}