You are here

function ctools_cleanstring_truncate in Chaos Tool Suite (ctools) 6

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

A friendly version of truncate_utf8.

Parameters

$string: The string to be truncated.

$length: An integer for the maximum desired length.

$separator: A string which contains the word boundary such as - or _.

Return value

The string truncated below the maxlength.

1 call to ctools_cleanstring_truncate()
ctools_cleanstring in includes/cleanstring.inc
Clean up a string value provided by a module.

File

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

Code

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