Trim string in Drupal 7 way

05/04/2017
Trim string in Drupal way

In this Drupal 7 snippet we will know how to trim any string in Drupal way using the truncate_utf8 function.

Code

truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1)

So for example we can do something like this:

$text = 'Here we have a large text saying nothing';
truncate_utf8($text, 12);
// Here we have a l
$text = 'Here we have a large text saying nothing';
truncate_utf8($text, 12, TRUE);
// Here we have a
$text = 'Here we have a large text saying nothing';
truncate_utf8($text, 12, TRUE, TRUE);
// Here we have...