You are here

function _pathauto_clean_separators in Pathauto 6.2

Same name and namespace in other branches
  1. 6 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 329
Miscellaneous functions for Pathauto.

Code

function _pathauto_clean_separators($string, $separator = NULL) {
  static $default_separator;
  if (!isset($separator)) {
    if (!isset($default_separator)) {
      $default_separator = variable_get('pathauto_separator', '-');
    }
    $separator = $default_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);
    }
  }
  return $output;
}