Want to get Lullabot article, videocast, and podcast announcements delivered right to your in-box?
Let us know your email address (we won't share it) and we'll let you know when anything exciting happens.
Trim A String To A Given Word Count
Lots of times when you're theming a site, you'll want to have a snippet of text in one place or another. Drupal doesn't really have a good way of doing this because different languages have different definitions of "words", and a space-character is not always the delimiter between words, as it is in most Latin-based languages.
But for those of us speaking languages that stick spaces between words, clients will often ask us to "just show the first 10 words" here or there. So here's a handy PHP function to do just that. And what's more, it can even add "…" at the end of the truncated text.
<?php
/**
* Trim a string to a given number of words
*
* @param $string
* the original string
* @param $count
* the word count
* @param $ellipsis
* TRUE to add "..."
* or use a string to define other character
* @param $node
* provide the node and we'll set the $node->
*
* @return
* trimmed string with ellipsis added if it was truncated
*/
function word_trim($string, $count, $ellipsis = FALSE){
$words = explode(' ', $string);
if (count($words) > $count){
array_splice($words, $count);
$string = implode(' ', $words);
if (is_string($ellipsis)){
$string .= $ellipsis;
}
elseif ($ellipsis){
$string .= '…';
}
}
return $string;
}
?>







Comments
Doesn't work with html
The only issue with this is it treats html tags such as
the same as regular words, which can be a pain if the string starts with <p> or <div> - you don't know whether to close the tag or which tag to use.
I ended up cloning Drupal's node_teaser() function in node.module and adding a parameter for the length of the the string.
Now that I think about it, adding strip_tags() at the beginning would help with that, though you would lose the author's formatting...
True
Yeah, I thought about that shortly after I posted. The real kicker is that if you truncate something like:
<strong>Imagine this is a really long text string. And in breaks | <-- right here.</strong>Then the entire rest of your page would be bold! But things start to get very complicated very fast when we need to essentially write an html parser to keep track of all of the tags, make sure that they're balanced, etc. etc. etc.
I may have to go back to the drawing board to come up with a bullet-proof handler for this. I'm just nervous that it's going to end up really complicated. Perhaps some regex magic can help us out...
Stay tuned.
also a Drupal teaser issue
The issue of unclosed tags in the teaser is a general problem for Drupal:
http://drupal.org/node/54833
Some examples of/links to tag-balancing code are there.
Will this work with CCK?
I tried doing this with CCK (latest CVS + Drupal 4.7), but am not sure how to go where to insert this. I tried inserting it in a text widget for a node i am desing to take in album reviews - but am not sure where to paste the code. Also, i'm not sure how to insert the 'number of words' to break at within the above snippet. Anyone know how?
ellipsis at end of sentence
An ellipsis at the end of a truncated sentence (as in this case) is actually made of four dots.
So:
"I am sitting... in an armchair on a cold day."
"I am sitting down casually in an armchair...."
Truncating node title fields
Does anyone have a quick variation on this snippet for trimming the length of node titles when displayed in views lists?
That would be a great trick. But what I'm not sure of is how to truncate the $title string without breaking it's anchor tag.
I'll play with this and post if I figure it out. But if someone's doing this already, I'd love some quick help.
Cheers,
Sean
css only teaser-solution
what i am doing is displaying my teaser-text in a <div> that's set to a specific height. Using the css overflow property i am "hiding" everything that's too long.
<div style="height: 200px; overflow: hidden;"><br />The teasertext goes here!!!</div>Post new comment