function truncate_utf8

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

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 calls to truncate_utf8()
aggregator_parse_feed in drupal-6.x/modules/aggregator/aggregator.module
Parse a feed and store its items.
comment_admin_overview in drupal-6.x/modules/comment/comment.admin.inc
Form builder; Builds the comment overview form for the admin.
dblog_overview in drupal-6.x/modules/dblog/dblog.admin.inc
Menu callback; displays a listing of log messages.
dblog_top in drupal-6.x/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 drupal-6.x/modules/node/node.module
Generate a teaser for a node body.

... See full list

File

drupal-6.x/includes/unicode.inc, line 233

Code

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;
}