Jump to content
smngy

Text output limiter

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

Share this post


Link to post
Share on other sites

Ah nuts, Figured it out.

Sorry I couldn’t remember the word for limiting text — truncate.

Here’s my code for anyone in a similar spot:

$summary = substr($p->field_name, 0, 200);
echo $summary;

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 5

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Actually I think all instances of $summary were supposed to be $text. Not sure what I was thinking, must have gotten a phone call in the middle or something. :)

Share this post


Link to post
Share on other sites

Our machines can do multitasking but we seem to be condemned to sequential operation  ;)

  • Like 1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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>";
                                 }    

Share this post


Link to post
Share on other sites

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? 

Share this post


Link to post
Share on other sites

Hello!

I'm quite new to PW (v3.0.42) and still a beginner with PHP.

I'm looking to output the first two letters of a page's title. (So, if $page->title = "Home" I want to output "Ho".

If someone could point me in the right direction that would be awesome!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...