Jump to content

Text output limiter


smngy
 Share

Recommended Posts

Hi All,

I could’ve sworn I had done this before but apparently not. I have a news page which displays 2 news entries. There’s a title field, an image and a blurb. I want to only show the first 100 words of that blurb field with a link below it to read more.

How to I limit the output of the text on my template?

Please and thank you.

Seán

  • Like 1
Link to comment
Share on other sites

Strangely, I was looking this up yesterday!
 

Doesn't that limit characters?
 

If you want to do words:

$text = implode(' ',array_slice(str_word_count($post->summary,1),0,$maxpostlength));
 

Where $maxpostlength is the number of words and $post->summary was the field I wanted to truncate.

  • Like 3
Link to comment
Share on other sites

  • 9 months later...

Hello all,

I'm pretty new to PW, coming over from the Joomla world.

In J! there is a truncate method that does just that: truncate a piece of text.

The problem with implode() or substr() is that it doesn't take html tags into account. So if I wanted to truncate a text that comes from a wysiwyg input, I might loose closing tags which can result in display/validation errors.

So basically my question is: Does PW API provide a truncate method and if not what are your favorite ways to get this done?

Thanks

Gerhard

EDIT:
There seems to be a very popular PHP function for Truncate string while preserving HTML tags and whole words.

And a slick jQuery plugin to limit text and expand to full length on click readmore: readmore.js

  • Like 1
Link to comment
Share on other sites

Usually when I need to truncate something it's because it needs to be a summary that fits appropriately in a defined area and consistently with other items of the same type. As a result, having HTML in the truncated text is no good, so I'll include a strip_tags() as part of the truncation process, then wrap it in my own <p> or what not. Most of my sites have a shared truncateText() function somewhere in there that works kind of like this. Maybe there should be one in PW's core, not sure. 

function truncateText($text, $maxlength = 200) {
  // truncate to max length
  $text = substr(strip_tags($text), 0, $maxlength);
  // check if we've truncated to a spot that needs further truncation
  if(strlen(rtrim($text, ' .!?,;')) == $maxlength) {
    // truncate to last word 
    $text = substr($text, 0, strrpos($text, ' ')); 
  }
  return trim($text); 
}

$summary = truncateText($page->body); 
echo "
  <a href='$page->url'>$page->title</a>
  <p class='summary'>$summary</p>
  ";

Edited by Pete
Fixed as per gerhard's post below.
  • Like 6
Link to comment
Share on other sites

Thank you, Ryan, for sharing this with us.

Using strip_tags and wrapping the output in <p> or whatever tags seems to be a great solution.

And I don't think this needs to go in the core because everybody can implement their own favourite solutions.

  • Like 1
Link to comment
Share on other sites

@Ryan

your code threw a little error because $summary was not defined. The first $text in the function should be $summary. Here's the complete fixed function:

function truncateText($text, $maxlength = 200) {
  // truncate to max length
  $summary = substr(strip_tags($text), 0, $maxlength);
  // check if we've truncated to a spot that needs further truncation
  if(strlen(rtrim($text, ' .!?,;')) == $maxlength) {
    // truncate to last word 
    $text = substr($summary, 0, strrpos($summary, ' ')); 
  }
  return trim($text); 
}

Cheers

gerhard

Link to comment
Share on other sites

  • 8 months later...

if anybody stumbles over this thread, here is the correct function (also gebeer's version has got a $text/$summary bug):

function truncateText($text, $maxlength = 200) {
  // truncate to max length
  $text = substr(strip_tags($text), 0, $maxlength);
  // check if we've truncated to a spot that needs further truncation
  if(strlen(rtrim($text, ' .!?,;')) == $maxlength) {
    // truncate to last word 
    $text = substr($text, 0, strrpos($text, ' ')); 
  }
  return trim($text); 
}

thank you ryan

  • Like 3
Link to comment
Share on other sites

  • 11 months later...
  • 8 months later...

If you are using delayed output hope this helps:

function truncateText($text, $maxlength = 350) {
  // truncate to max length
  $text = substr(strip_tags($text), 0, $maxlength);
  // check if we've truncated to a spot that needs further truncation
  if(strlen(rtrim($text, ' .!?,;')) == $maxlength) {
    // truncate to last word
    $text = substr($text, 0, strrpos($text, ' '));
  }
  return trim($text);
}

// NEWS SUMMARY
$features = $pages->find('template=news-page, limit=10, sort=-news_date');
foreach($features as $feature) {
                            
$content.="    <div class='row'>
               <div class='4u 12u$(xsmall)'>
                   <p>
                   <a href='{$feature->url}' title='{$feature->title}'><img src='{$feature->image->url}' alt='{$feature->title}' /></a>
                   </p>
               </div>
               <div class='8u 12u$(xsmall)'>
                <h2><a href='{$feature->url}'>{$feature->title}</a> - ({$feature->news_date})</h2>
                <p>";
$content.=truncateText($feature->summary);
$content.="...</p>
                <p><a href='{$feature->url}' class='button'>More</a></p>
               </div>
               </div>
               <hr>";
                                 }    
Link to comment
Share on other sites

  • 7 months later...

Hello everyone. I am at a point of my template where I need to limit the text output to specific number of words, so so far I found two good suggestions - one is of Ryan and others in this topic and other one is of Soma. I tested both solutions and they seem to work, however I am just curious should I use that or there is something that have been implemented into the core?

Honestly, I would think it would be nice to have a simple hook added which would make the text output limiter work with PW out of the box, however I am just a new user of PW so can't think of all the pros and cons of having done that.

So far I added a functions.php file in my theme files and call it where the function is needed. One thing I am wondering is about the right way of including the file. Should I go with include in the templates where I need the text output to be limited or I should use require once? 

Link to comment
Share on other sites

  • 1 month later...
  • 4 years later...
On 11/30/2013 at 5:54 AM, ryan said:

 

function truncateText($text, $maxlength = 200) {
  // truncate to max length
  $text = substr(strip_tags($text), 0, $maxlength);
  // check if we've truncated to a spot that needs further truncation
  if(strlen(rtrim($text, ' .!?,;')) == $maxlength) {
    // truncate to last word 
    $text = substr($text, 0, strrpos($text, ' ')); 
  }
  return trim($text); 
}

$summary = truncateText($page->body); 
echo "
  <a href='$page->url'>$page->title</a>
  <p class='summary'>$summary</p>
  ";

I've been using this approach for just what you mentioned... shortening blurbs and tucking them into a page with several other items of like type... but this works great but launders out numbers... 

So something like "Average temperature data for each month from 1880 to 1990 to is displayed"...

Comes out as "Average temperature data for each month from to to is displayed"...

I see a code mention of mixed, for a mixed string, but can't figure out how to incorporate it...

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...