truncate_utf8

Versions
4.6
truncate_utf8($string, $len, $wordsafe = FALSE)
4.7 – 7
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.

▾ 13 functions call truncate_utf8()

aggregator_parse_feed in modules/aggregator/aggregator.parser.inc
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.
comment_submit in modules/comment/comment.module
Prepare a comment for submission.
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.
field_ui_existing_field_options in modules/field_ui/field_ui.admin.inc
Return an array of existing field to be added to a bundle.
search_excerpt in modules/search/search.module
Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.
text_summary in modules/field/modules/text/text.module
Generate a trimmed, formatted version of a text field value.
_book_toc_recurse in modules/book/book.module
A recursive helper function for book_toc().
_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.

Code

includes/unicode.inc, line 231

<?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;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.