4.6.x common.inc truncate_utf8($string, $len, $wordsafe = FALSE)
4.7.x unicode.inc truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)
5.x unicode.inc truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)
6.x unicode.inc truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)
7.x 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.


$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 calls to 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.

... See full list


includes/unicode.inc, line 233


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;


slippast’s picture

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.