Chapter Three LLC

HOWTO: Quickly Truncate a Block of Text

Farsheed Hamidi-Toosi

When theming things I run into situations where a block of text is too long and I need a really quick way to truncate text and append an ellipses or custom trailing text. The built in drupal teaser truncates text in a much more logical manner (watches out for html tags and the like) while this php snippet is simpler and meant to run on pure text. You can do strip_tags() if you want to remove all the html tags before running this code on your piece of text. I often just drop this into my template.php and use it in various spots in the theme.

<?php
/**
* Truncate the string if it is beyond a certain $length and append with an ellipses or custom text
* $length is the number of characters allowed before truncating
* $append is appended to the truncated string
*/
function your_theme_custom_truncate($string = '', $length = 30, $append = '&#8230;') {
  return
strlen($string) > $length ? trim(substr($string, 0, $length)) . $append : $string;
}
?>

Before:

Suspendisse potenti. Ut tempus auctor libero. Aliquam molestie dolor quis lectus. Curabitur et erat eget lorem nonummy ultrices. Mauris interdum. Etiam imperdiet viverra purus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Mauris sem mauris, feugiat at, dictum vitae, placerat nec, odio. Nulla fringilla. Morbi justo nulla, commodo quis, ultricies a, eleifend eu, ante. Morbi eget erat eu ante tincidunt interdum. Nullam at est. Nulla ultrices pede vitae sapien bibendum bibendum. Quisque turpis enim, ullamcorper at, fermentum quis, vulputate quis, libero. In facilisis, dui eget accumsan molestie, justo metus tempor quam, eu tempor ipsum eros a urna. Donec aliquet. Curabitur condimentum volutpat augue. Praesent bibendum commodo enim.

After:

Suspendisse potenti. Ut tempus auctor libero. Aliquam molestie dolor quis lectus. Curabitur
et erat eget lorem nonummy ul…

Actually, Drupal API already

Actually, Drupal API already has a similar function to what I proposed and it even watches out for word chunks and outputs … (ellipses at the end). The hidden secrets of Drupal…

http://api.drupal.org/api/5/function/truncate_utf8

truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)

Posted by Farsheed Hamidi... | Jul. 27th, 2007 @ 1:11pm | Link to this Comment

Truncating teaser for preview & views

This is interesting. The trouble I have found with strip_tags and teasers is that when I use it in my node.tpl file, it strips tags also from the ‘full preview version’ of node preview as well - which is kind of ugly, not to mention disconcerting, for a user.

if($page == 0){
   $content=strip_tags($content);
}

If I wished to strip line breaks from the teaser, how is this done without affecting ‘full preview’?

I don’t mind editing the node module as I expect also it is more efficient to write this stripped text to the database - rather than processing it on each call?

Any suggestions?

Posted by Anonymous (not verified) | Jun. 10th, 2007 @ 2:37am | Link to this Comment

node_teaser too

In cases where I don’t want to strip tags, I’ve copied Drupal’s node_teaser into a new function, adding a third argument that lets me pass along a character length that I like, and removing the line where it gets $size from variable_get.

http://api.drupal.org/api/5/function/node_teaser

This is a quick way to be able to handle formatted text. It makes for a longer custom function, but works pretty well too.

Posted by Josh Koenig | Apr. 27th, 2007 @ 9:52am | Link to this Comment

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br/> <br /> <p> <img> <blockquote> <i> <b> <u>
  • Lines and paragraphs break automatically.
  • SmartyPants will translate ASCII punctuation characters into “smart” typographic punctuation HTML entities.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options