function views_trim_text

3.x views.module views_trim_text($alter, $value)
2.x views.module views_trim_text($alter, $value)

Trim the field down to the specified length.

Parameters

$alter:

  • max_length: Maximum lenght of the string, the rest gets truncated.
  • word_boundary: Trim only on a word boundary.
  • ellipsis: Trim only on a word boundary.
  • html: Take sure that the html is correct.
1 call to views_trim_text()
views_handler_field::render_trim_text in handlers/views_handler_field.inc
Trim the field down to the specified length.

File

./views.module, line 1345
Primarily Drupal hooks and global API functions to manipulate views.

Code

function views_trim_text($alter, $value) {
  if (drupal_strlen($value) > $alter['max_length']) {
    $value = drupal_substr($value, 0, $alter['max_length']);
    // TODO: replace this with cleanstring of ctools
    if (!empty($alter['word_boundary'])) {
      $regex = "(.*)\b.+";
      if (function_exists('mb_ereg')) {
        mb_regex_encoding('UTF-8');
        $found = mb_ereg($regex, $value, $matches);
      }
      else {
        $found = preg_match("/$regex/us", $value, $matches);
      }
      if ($found) {
        $value = $matches[1];
      }
    }
    // Remove scraps of HTML entities from the end of a strings
    $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));

    if (!empty($alter['ellipsis'])) {
      $value .= '...';
    }
  }
  if (!empty($alter['html'])) {
    $value = _filter_htmlcorrector($value);
  }

  return $value;
}