You are here

function _pathauto_clean_separators in Pathauto 6

Same name and namespace in other branches
  1. 6.2 pathauto.inc \_pathauto_clean_separators()
  2. 7 pathauto.inc \_pathauto_clean_separators()

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

Parameters

$string: The string to clean path separators from.

$separator: The path separator to use when cleaning.

Return value

The cleaned version of the string.

See also

pathauto_cleanstring()

pathauto_clean_alias()

2 calls to _pathauto_clean_separators()
pathauto_cleanstring in ./pathauto.inc
Clean up a string segment to be used in an URL alias.
pathauto_clean_alias in ./pathauto.inc
Clean up an URL alias.

File

./pathauto.inc, line 194
Miscellaneous functions for Pathauto.

Code

function _pathauto_clean_separators($string, $separator = NULL) {
  $output = $string;
  if (!isset($separator)) {
    $separator = variable_get('pathauto_separator', '-');
  }

  // 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;
}