Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

Drupal Module Development Deep Dive Week

London, UK
September 20-24, 2010

Trim A String To A Given Word Count

Article by Jeff RobbinsJune 22, 2006 - 5:46pm

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 .= '&hellip;';
    }
  }
  return
$string;
}
?>

Comments

Derek Scruggs (not verified) on June 22, 2006 - 9:09pm

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

June 23, 2006 - 6:26am Jeff Robbins

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.

Peter (not verified) on June 23, 2006 - 4:56pm

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.

Morgan (not verified) on September 30, 2006 - 5:36pm

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?

matthew (not verified) on October 27, 2006 - 10:41am

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

Anonymous (not verified) on November 9, 2006 - 1:03pm

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

Alcyon (not verified) on September 9, 2008 - 8:03pm

It's a bit dated - but just in case...

It's a bit dated - but I need to do exactly the same thing - that is to 'trim' long titles for displaying on Views block. I am not a coder - has anyone found a way to do this?
It looks like the Computed Field module could be of help - but again, only if you know what do with it :) and I don't :(
So, if anyone stumbles on to this, I would appreciate a reply.
BTW, I'm trying to use Views 2 on a Drupal 6 installation.

phifeshaheed (not verified) on April 23, 2009 - 10:08am

It's been a while since this

It's been a while since this has been posted but i searching the web for solutions to do this trim the title in the a list of node block generated by views... i tried to do such thing with the options given by the views module but i haven't been sucessfull so far

Chris (not verified) on December 12, 2006 - 4:45am

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>

About this 'bot

Jeff Robbins

Jeff Robbins is co-founder and CEO of Lullabot. He worked at O'Reilly as an illustrator and systems administrator as the World Wide Web came into being. He was involved in the early stages of the first commercial website, O'Reilly's Global Network Navigator, but left to start one of the first web development companies...

more

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Photo galleries with Views Attach

Article 6.01.2009

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Announcing BeautyTips, a jQuery Tooltip Plugin

Article 10.20.2008
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List