Same name and namespace in other branches
  1. 4.6.x includes/common.inc \truncate_utf8()
  2. 4.7.x includes/unicode.inc \truncate_utf8()
  3. 6.x includes/unicode.inc \truncate_utf8()
  4. 7.x includes/unicode.inc \truncate_utf8()

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

If the end position is in the middle of a UTF-8 sequence, it scans backwards until the beginning of the byte sequence.

Use this function whenever you want to chop off a string at an unsure location. On the other hand, if you're sure that you're splitting on a character boundary (e.g. after using strpos() or similar), you can safely use substr() instead.

Parameters

$string: The string to truncate.

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

$wordsafe: Flag to truncate at nearest space. Defaults to FALSE.

Return value

The truncated string.

10 calls to truncate_utf8()
comment_admin_overview in modules/comment/comment.module
mime_header_encode in includes/unicode.inc
Encodes MIME/HTTP header values that contain non-ASCII, UTF-8 encoded characters.
node_teaser in modules/node/node.module
Automatically 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.
watchdog_overview in modules/watchdog/watchdog.module
Menu callback; displays a listing of log messages.

... See full list

File

includes/unicode.inc, line 210

Code

function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
  $slen = strlen($string);
  if ($slen <= $len) {
    return $string;
  }
  if ($wordsafe) {
    $end = $len;
    while ($string[--$len] != ' ' && $len > 0) {
    }
    if ($len == 0) {
      $len = $end;
    }
  }
  if (ord($string[$len]) < 0x80 || ord($string[$len]) >= 0xc0) {
    return substr($string, 0, $len) . ($dots ? ' ...' : '');
  }
  while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xc0) {
  }
  return substr($string, 0, $len) . ($dots ? ' ...' : '');
}