You are here

function advanced_text_formatter_trim_text in Advanced Text Formatter 7

Same name and namespace in other branches
  1. 8 advanced_text_formatter.module \advanced_text_formatter_trim_text()
  2. 2.1.x advanced_text_formatter.module \advanced_text_formatter_trim_text()
  3. 2.0.x advanced_text_formatter.module \advanced_text_formatter_trim_text()

Trim text.

Parameters

string $text: The string is being trimmed.

array $options: An associative array containing:

  • html: TRUE means that text is in HTML.
  • max_length: The maximum number of characters the a field can be.
  • word_boundary: If checked, this field be trimmed only on a word boundary.
  • ellipsis: If TRUE, a "..." will be added if a field was trimmed.

Return value

string The trimmed string.

1 call to advanced_text_formatter_trim_text()
advanced_text_formatter_field_formatter_view in ./advanced_text_formatter.module
Implements hook_field_formatter_view().

File

./advanced_text_formatter.module, line 437
Advanced Text Formatter

Code

function advanced_text_formatter_trim_text($text, $options) {
  if (!isset($options['html'])) {
    $options['html'] = TRUE;
    $options['max_length'] = _advanced_text_formatter_get_html_length($text, $options['max_length']);
  }
  if (drupal_strlen($text) > $options['max_length']) {
    $text = drupal_substr($text, 0, $options['max_length']);

    // TODO: replace this with cleanstring of ctools.
    if (!empty($options['word_boundary'])) {
      $regex = "(.*)\\b.+";
      if (function_exists('mb_ereg')) {
        mb_regex_encoding('UTF-8');
        $found = mb_ereg($regex, $text, $matches);
      }
      else {
        $found = preg_match("/{$regex}/us", $text, $matches);
      }
      if ($found) {
        $text = $matches[1];
      }
    }

    // Remove scraps of HTML entities from the end of a strings.
    $text = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $text));
    if (!empty($options['ellipsis'])) {
      $text .= t('...');
    }
  }
  if (!empty($options['html'])) {
    $text = _filter_htmlcorrector($text);
  }
  return $text;
}