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; } /** * A friendly version of truncate_utf8. * * @param $string * The string to be truncated. * @param $length * An integer for the maximum desired length. * @param $separator * A string which contains the word boundary such as - or _. * * @return * The string truncated below the maxlength. */ function ctools_cleanstring_truncate($string, $length, $separator) { if (drupal_strlen($string) > $length) { $string = drupal_substr($string, 0, $length + 1); // leave one more character if ($last_break = strrpos($string, $separator)) { // space exists AND is not on position 0 $string = substr($string, 0, $last_break); } else { $string = drupal_substr($string, 0, $length); } } return $string; }