Skip to content

Instantly share code, notes, and snippets.

@pepeloper
Created March 20, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepeloper/ea7dffb9567a3ac07705f0016ee4a6ac to your computer and use it in GitHub Desktop.
Save pepeloper/ea7dffb9567a3ac07705f0016ee4a6ac to your computer and use it in GitHub Desktop.
Trim string in Drupal way

Drupal Snippet

In this 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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment