How to easily trim text to a certain length

If you have Views' module installed, you can use its views_trim_text() function to programmatically trim field value (or any other text for that matter) to a specified length from your code.

If you have Views module installed, you can use its views_trim_text() function to programmatically trim field value (or any other text for that matter) to a specified length from your code.

Usage:

views_trim_text($alter, $value)

where $alter array consists of the following elements:

  • max_length - maximum lenght of the string, the rest gets truncated,
  • word_boundary - trim only on a word boundary,
  • ellipsis - show an ellipsis (...) at the end of the trimmed string,
  • html - take sure that the html is correct.