You are here

function ctools_cleanstring in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/cleanstring.inc \ctools_cleanstring()

Clean up a string value provided by a module.

Resulting string contains only alphanumerics and separators.

Parameters

$string: A string to clean.

$settings: An optional array of settings to use.

  • 'clean slash': If set, slashes will be cleaned. Defaults to TRUE, so you have to explicitly set this to FALSE to not clean the slashes.
  • 'ignore words': Set to an array of words that will be removed rather than made safe. Defaults to an empty array.
  • 'separator': Change spaces and untranslatable characters to this character. Defaults to '-'.
  • 'replacements': An array of direct replacements to be made that will be implemented via strtr(). Defaults to an empty array.
  • 'transliterate': If set, use the transliteration replacements. If set to an array, use these replacements instead of the defaults in CTools. Defaults to FALSE.
  • 'reduce ascii': If set to TRUE further reduce to ASCII96 only. Defaults to TRUE.
  • 'max length': If set to a number, reduce the resulting string to this maximum length. Defaults to no maximum length.
  • 'lower case': If set to TRUE, convert the result to lower case. Defaults to false.

These settings will be passed through drupal_alter.

Return value

The cleaned string.

2 calls to ctools_cleanstring()
ctools_stylizer_edit_style_form_choose in includes/stylizer.inc
Choose which plugin to use to create a new style.
ctools_stylizer_get_css_class in includes/stylizer.inc
Get the class to use for a stylizer plugin.

File

includes/cleanstring.inc, line 98
Helper class to clean strings to make them URL safe and translatable.

Code

function ctools_cleanstring($string, $settings = array()) {
  $settings += array(
    'clean slash' => TRUE,
    'ignore words' => array(),
    'separator' => '-',
    'replacements' => array(),
    'transliterate' => FALSE,
    'reduce ascii' => TRUE,
    'max length' => FALSE,
    'lower case' => FALSE,
  );

  // Allow modules to make other changes to the settings.
  if (isset($settings['clean id'])) {
    drupal_alter('ctools_cleanstring_' . $settings['clean id'], $settings);
  }
  drupal_alter('ctools_cleanstring', $settings);
  $output = $string;

  // Do any replacements the user selected up front.
  if (!empty($settings['replacements'])) {
    $output = strtr($output, $settings['replacements']);
  }

  // Remove slashes if instructed to do so.
  if ($settings['clean slash']) {
    $output = str_replace('/', '', $output);
  }
  if (!empty($settings['transliterate']) && module_exists('transliteration')) {
    $output = transliteration_get($output);
  }

  // Reduce to the subset of ASCII96 letters and numbers
  if ($settings['reduce ascii']) {
    $pattern = '/[^a-zA-Z0-9\\/]+/';
    $output = preg_replace($pattern, $settings['separator'], $output);
  }

  // Get rid of words that are on the ignore list
  if (!empty($settings['ignore words'])) {
    $ignore_re = '\\b' . preg_replace('/,/', '\\b|\\b', $settings['ignore words']) . '\\b';
    if (function_exists('mb_eregi_replace')) {
      $output = mb_eregi_replace($ignore_re, '', $output);
    }
    else {
      $output = preg_replace("/{$ignore_re}/i", '', $output);
    }
  }

  // Always replace whitespace with the separator.
  $output = preg_replace('/\\s+/', $settings['separator'], $output);

  // In preparation for pattern matching,
  // escape the separator if and only if it is not alphanumeric.
  if (isset($settings['separator'])) {
    if (preg_match('/^[^' . CTOOLS_PREG_CLASS_ALNUM . ']+$/uD', $settings['separator'])) {
      $seppattern = $settings['separator'];
    }
    else {
      $seppattern = '\\' . $settings['separator'];
    }

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

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

  // Enforce the maximum component length
  if (!empty($settings['max length'])) {
    $output = ctools_cleanstring_truncate($output, $settings['max length'], $settings['separator']);
  }
  if (!empty($settings['lower case'])) {
    $output = drupal_strtolower($output);
  }
  return $output;
}