Community Documentation

truncate_utf8

5 unicode.inc truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)
6 unicode.inc truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)
7 unicode.inc truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1)
8 unicode.inc truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1)

Truncate a UTF-8-encoded string safely to a number of characters.

Parameters

$string: The string to truncate.

$len: An upper limit on the returned string length.

$wordsafe: Flag to truncate at last space within the upper limit. Defaults to FALSE.

$dots: Flag to add trailing dots. Defaults to FALSE.

Return value

The truncated string.

▾ 12 functions call truncate_utf8()

aggregator_parse_feed in modules/aggregator/aggregator.module
Parse a feed and store its items.
comment_admin_overview in modules/comment/comment.admin.inc
Form builder; Builds the comment overview form for the admin.
dblog_overview in modules/dblog/dblog.admin.inc
Menu callback; displays a listing of log messages.
dblog_top in modules/dblog/dblog.admin.inc
Menu callback; generic function to display a page of the most frequent dblog events of a specified type.
node_teaser in modules/node/node.module
Generate a teaser for a node body.
search_excerpt in modules/search/search.module
Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.
_book_toc_recurse in modules/book/book.module
A recursive helper function for book_toc().
_comment_form_submit in modules/comment/comment.module
Prepare a comment for submission.
_locale_translate_seek in includes/locale.inc
Perform a string search and display results in a table
_menu_parents_recurse in modules/menu/menu.module
Recursive helper function for menu_parent_options().
_search_index_truncate in modules/search/search.module
Helper function for array_walk in search_index_split.
_statistics_link in modules/statistics/statistics.module
It is possible to adjust the width of columns generated by the statistics module.

File

includes/unicode.inc, line 233

Code

<?php
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {

  if (drupal_strlen($string) <= $len) {
    return $string;
  }

  if ($dots) {
    $len -= 4;
  }

  if ($wordsafe) {
    $string = drupal_substr($string, 0, $len + 1); // leave one more character
    if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
      $string = substr($string, 0, $last_space);
    }
    else {
      $string = drupal_substr($string, 0, $len);
    }
  }
  else {
    $string = drupal_substr($string, 0, $len);
  }

  if ($dots) {
    $string .= ' ...';
  }

  return $string;
}
?>

Comments

Truncate at end of sentence

To truncate at the end of a sentence, which is a nice and clean alternative, use the text_summary() function.

It's a little hard to track that function down since due to the name, super useful though.

Login or register to post comments