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...

 

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.