You are here

function truncate_utf8 in Drupal 6

Same name and namespace in other branches
  1. 4 includes/unicode.inc \truncate_utf8()
  2. 5 includes/unicode.inc \truncate_utf8()
  3. 7 includes/unicode.inc \truncate_utf8()

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

File

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