function ctools_cleanstring_truncate in Chaos Tool Suite (ctools) 7
Same name and namespace in other branches
- 6 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 192 - 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) {
// Leave one more character.
$string = drupal_substr($string, 0, $length + 1);
// Space exists AND is not on position 0.
if ($last_break = strrpos($string, $separator)) {
$string = substr($string, 0, $last_break);
}
else {
$string = drupal_substr($string, 0, $length);
}
}
return $string;
}