You are here

function calendar_trim_text in Calendar 5.2

Trim a post to a certain number of characters, removing all HTML.

1 call to calendar_trim_text()
theme_calendar_views_field in ./calendar.theme
Wrapper around views_theme_field() to properly format the dates in the view. Use the usual views field formatting for all other fields.

File

./calendar.module, line 994
Adds calendar filtering and displays to Views.

Code

function calendar_trim_text($text, $length = 128) {
  $original_text = $text;

  // remove any HTML or line breaks so these don't appear in the text
  $text = trim(str_replace(array(
    "\n",
    "\r",
  ), ' ', strip_tags($text)));
  $text = trim(substr($text, 0, $length));

  // only truncate if original text is longer than the length we want
  if (strlen($original_text) > $length) {
    $lastchar = substr($text, -1, 1);

    // check to see if the last character in the title is a non-alphanumeric character, except for ? or !
    // if it is strip it off so you don't get strange looking titles
    if (preg_match('/[^0-9A-Za-z\\!\\?]/', $lastchar)) {
      $text = substr($text, 0, -1);
    }

    // ? and ! are ok to end a title with since they make sense
    if ($lastchar != '!' and $lastchar != '?') {
      $text .= '...';
    }
  }
  return $text;
}