You are here

function oa_core_trim_text in Open Atrium Core 7.2

Trim text to a specified length Similar to views_trim_text or the Smart Trim module but includes consistent options for removing tags needed by OA2

2 calls to oa_core_trim_text()
oa_core_get_summary in includes/oa_core.util.inc
Helper function to return the summary of a node or the trimmed body text
oa_news_preprocess_views_view_fields in modules/oa_news/oa_news.module
Implements hook_preprocess_views_view_fields(). Perform field-level replacement/processing here

File

includes/oa_core.util.inc, line 1475
Code for Utility functions for OpenAtrium spaces

Code

function oa_core_trim_text($value, $more_link = '', $max_length = NULL) {
  $trimmed = FALSE;

  // Remove and replace some HTML tags
  $value = oa_core_replace_tags($value, array(
    'h1' => 'h4',
    'h2' => 'h4',
    'h3' => 'h4',
  ));
  $value = strip_tags($value, '<br><p><h1><h2><h3><h4><h5><h6><ol><ul><li><dl><dd><dt><a><b><i><strong><em><table><tbody><th><tr><td>');
  $max_length = isset($max_length) ? $max_length : variable_get('teaser_length', 600);
  if (drupal_strlen($value) > $max_length) {
    $value = drupal_substr($value, 0, $max_length);

    // only trim on word boundries
    $regex = "(.*)\\b.+";
    if (function_exists('mb_ereg')) {
      mb_regex_encoding('UTF-8');
      $matches = array();
      $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));
    $value .= '…';
    $trimmed = TRUE;
  }

  // add any needed closing tags.
  $value = _filter_htmlcorrector($value);
  if ($trimmed) {
    if (!empty($more_link)) {
      $value .= '<span class="more-link">' . l(t('more…'), $more_link) . '</span>';
    }
  }
  return $value;
}