HOWTO: Quickly Truncate a Block of Text
Navigation : Back to Blog
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.
<span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/** <br /> * Truncate the string if it is beyond a certain $length and append with an ellipses or custom text<br /> * $length is the number of characters allowed before truncating<br /> * $append is appended to the truncated string<br /> */<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">your_theme_custom_truncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">$length </span><span style="color: #007700">= </span><span style="color: #0000BB">30</span><span style="color: #007700">, </span><span style="color: #0000BB">$append </span><span style="color: #007700">= </span><span style="color: #DD0000">'…'</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">) > </span><span style="color: #0000BB">$length </span><span style="color: #007700">? </span><span style="color: #0000BB">trim</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$length</span><span style="color: #007700">)) . </span><span style="color: #0000BB">$append </span><span style="color: #007700">: </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span>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…
Comments
Post a comment
Presentations
What we've coded
Videos
Upcoming workshops
-
San Francisco, CAJune 18, 2012 - June 19, 2012
-
San Francisco, CAJune 20, 2012 - June 21, 2012
-
San Francisco, CAJune 22, 2012
-
Alexandria, VAJuly 24, 2012 - July 25, 2012








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.
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?
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)
Several times I had to truncate a text not to break the layout. I created a JQuery function to solve this problem. The search function by specifying a CSS class and checks in an HTML element attribute in the limit (in characters).
HTML
view sourceprint?1 The text will be truncated according to the limit.
jQuery
view sourceprint?01 $().ready(function()
02 truncarTextos();
03 );
04
05 function truncarTextos()
06 $('.truncar').each(function()
07 truncarTexto($(this));
08 );
09
10
11 function truncarTexto(elementoHtml)
12 var limite = elementoHtml.attr("limite")
13
14 if(elementoHtml.html().length > limite)
15
16 elementoHtml.html(elementoHtml.html().substring(0,limite) + "...");
17
18
by como ganhar na lotomania