smngy Posted February 20, 2013 Share Posted February 20, 2013 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 1 Link to comment Share on other sites More sharing options...
smngy Posted February 20, 2013 Author Share Posted February 20, 2013 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; Link to comment Share on other sites More sharing options...
Joss Posted February 20, 2013 Share Posted February 20, 2013 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. 3 Link to comment Share on other sites More sharing options...
gebeer Posted November 25, 2013 Share Posted November 25, 2013 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 1 Link to comment Share on other sites More sharing options...
Macrura Posted November 25, 2013 Share Posted November 25, 2013 i use this a lot: http://processwire.com/talk/topic/3429-how-to-set-text-linecharacter-limits-in-templates/?p=33779 1 Link to comment Share on other sites More sharing options...
ryan Posted November 30, 2013 Share Posted November 30, 2013 (edited) 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 October 26, 2015 by Pete Fixed as per gerhard's post below. 6 Link to comment Share on other sites More sharing options...
gebeer Posted November 30, 2013 Share Posted November 30, 2013 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. 1 Link to comment Share on other sites More sharing options...
gebeer Posted December 3, 2013 Share Posted December 3, 2013 @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 More sharing options...
ryan Posted December 11, 2013 Share Posted December 11, 2013 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. Link to comment Share on other sites More sharing options...
gebeer Posted December 11, 2013 Share Posted December 11, 2013 Our machines can do multitasking but we seem to be condemned to sequential operation 1 Link to comment Share on other sites More sharing options...
bernhard Posted August 29, 2014 Share Posted August 29, 2014 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 3 Link to comment Share on other sites More sharing options...
m-artin Posted August 8, 2015 Share Posted August 8, 2015 By the way, if you also have to deal with asian languages, like chinese or japanese – have a look here: http://stackoverflow.com/questions/2154220/truncate-a-multibyte-string-to-n-chars Link to comment Share on other sites More sharing options...
dab Posted April 24, 2016 Share Posted April 24, 2016 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 More sharing options...
MilenKo Posted December 19, 2016 Share Posted December 19, 2016 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 More sharing options...
szabesz Posted December 20, 2016 Share Posted December 20, 2016 Hi, Is it the one you found from Ryan? https://processwire.com/blog/posts/pw-3.0.28/ As you can see, it is quite recent which suggests there in no core support at the moment. 1 Link to comment Share on other sites More sharing options...
VirtuallyCreative Posted February 9, 2017 Share Posted February 9, 2017 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! Link to comment Share on other sites More sharing options...
Robin S Posted February 9, 2017 Share Posted February 9, 2017 Welcome @VirtuallyCreative! substr() is the function that is usually used to get a portion of a string in PHP. substr($page->title, 0, 2) 2 Link to comment Share on other sites More sharing options...
VirtuallyCreative Posted February 9, 2017 Share Posted February 9, 2017 @Robin S Thanks! That did the trick. Link to comment Share on other sites More sharing options...
hollyvalero Posted July 30, 2021 Share Posted July 30, 2021 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 More sharing options...
Robin S Posted July 31, 2021 Share Posted July 31, 2021 @hollyvalero, after these posts were written a powerful truncate feature was added to the core as a sanitizer method. https://processwire.com/api/ref/sanitizer/truncate/https://processwire.com/blog/posts/processwire-3.0.101-core-updates/ 1 Link to comment Share on other sites More sharing options...
hollyvalero Posted July 31, 2021 Share Posted July 31, 2021 @Robin S - thanks! That worked beautifully. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now