Limit amount of text shown

23 posts by 6 authors in: Forums > CMS Builder
Last Post: November 17, 2010   (RSS)

By grauchut - November 4, 2008

I tried the code and it didn't work. how does it go. Does the function go at the top? Sorry for being non php smart. Thanks Again

// Usage: < ?php print maxWords($content, 25); ? >
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}

Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - November 5, 2008

Yes, it goes at the top. Here's an example:

<?php

// Usage: < ?php print maxWords($content, 25); ? >
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>

<?php
$record['content'] = "The quick brown fox jumps over the lazy dog.";
echo maxWords($record['content'], 5);
?>


The first part goes at the top, the second part can go where ever you want. Note that you don't need to set $record['content'] (or whatever your variable is named) if it's already set somewhere else.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

By grauchut - November 13, 2008

Dave sorry for the delay. It works great thanks again
Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - November 14, 2008

Great! Thanks for posting back and letting us know! :)
Dave Edis - Senior Developer
interactivetools.com

By studio-a - January 29, 2009

Hello,

Thanks for the post, this works nice for abstracts and short news links.

I wanted to extend this function to use within a Detail Page. For example, if an article has over 1500 words and I wanted to limit a Detail Page to only 500 words with an HTML link to continue reading the rest of the article. How can we limit the text for one page to continue with the remaining article text on a second or even third page via an HTML link “dynamically.”

I have a feeling there has to be a different function to use within Detail Page 2 to pick up the word count from 501 to 1000 words, but I am not clear how to approach this problem. Is there another thread that covers this topic or can you point me in the right direction?

Thanks for your help.

Re: [studio-a] Limit amount of text shown

By eduran582 - January 29, 2009

Dave,

I looked through the code and I'm pretty sure I understand how it works. My question is within the substr function (I think) and would like to know if there's a way to search for a certain character (say, a slash) within the string and then display the contents up to that point.

I have a string with some text and I'd like to add some numbers after the text that would be separate from the first part. The original text would be displayed one place and I'd like to display just the first part (up to the slash, in this case) in another area. Example: complete text: "Jan 29, 2009 /20", would like to display "Jan 29, 2009" in a separate area. Do-able?

Thanks.

Eric

Re: [eduran582] Limit amount of text shown

By eduran582 - January 29, 2009

Dave,

Disregard my request above...I went to the php link you gave and more or less found what I needed. In a nutshell, I found and used the php strrev command to find what I needed:

<?php foreach (range(1,50) as $num): ?>
<?php $stringA = $registrationRecord["list$num"] ?>
<?php $toFind = " " ?>
<?php if ([url "mailto:!@$registrationRecord["list$num"]!@$registrationRecord["list$num[/url]"]) { break; } ?>
<option><?php echo strrev( strchr(strrev($stringA),$toFind) ) ?><br/></option>
<?php endforeach ?>

<?php if (!$registrationRecord): ?>
(Pending)<br/><br/>
<?php endif ?>


Result displays just the date ("Jan 29, 2009"). I had to make sure I stipulated in the instructions to the user to make sure there is a space after the date and before the slash.

Hope I saved you some trouble. It's not like you have anything else to do... [;)]

Eric

Re: [studio-a] Limit amount of text shown

By Dave - January 30, 2009

Hi studio-a,

It's possible, but would require some custom coding. What would code up next would be how to detect if there was more pages, displaying prev/next links, etc. It gets a bit more complicated. We could build something custom for you to do this if needed. Feel free to email us if you'd like to discuss that more.

Eric: Glad you got it figured out! :)

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [zick] Limit amount of text shown

By Dave - November 16, 2010

Hi Zick,

It can be tricky splitting up HTML content because you can end up with partial or incomplete html and spacing issues as you mention.

If you can do the following, though, I can take a look and try to adjust your code:
- Post the function you want to use (which of the two?)
- Post a short snippet of HTML that demonstrates the problem.
- Post the HTML you are getting back
- Post the HTML you would like to get back instead.

If you can do that it will make it easy for me to re-create the issue locally and suggest some code changes.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com