Limit amount of text shown

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

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

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

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

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

By Mikey - November 16, 2010 - edited: November 16, 2010

Dave,
I've tried two different maxWord options, but neither seem to be working 100% for me. I've tried to explain my problem below, can you shed some light on what I need to do to get maxWords working for me?

Using the code below works, up until there's a <br/> break or <ul><li> bullet list. In the wysiwyg if there's a <br/> break to start a new sentence directly below the first, the <br/> break is lost and the new sentence does not have a space after the period where the first sentence ended and the new sentence begins. Example (... this sentence has ended.This sentence has no space before previous sentence period.)

And when there's a <ul> <li> bullet list the text within the bullet list looses it's paragraph formatting completely and the font style does not match any of the other article's font style using maxWords.

I've attached a snap shot with the problem areas highlighted.

Code Option One:
<?PHP
// function maxWords($textOrHtml, $maxWords) {
// $text=str_replace("<p>","*P*",$textOrHtml);
// $text= str_replace("</p>","*/P*",$text);
// $text = strip_tags($text);
// $words = preg_split("/\s+/", $text, $maxWords+1);
// if (count($words) > $maxWords) { unset($words[$maxWords]); }
// $output = join(' ', $words);
// $output=str_replace("*P*","<p>",$output);
// $output=str_replace("*/P*","</p>",$output);
// $output.="</p>";
//
// return $output;
// }
?>


So then I tried using the code below and that fixes the text font style formatting for <ul> <li> by eliminating it altogether and making it read as a sentence with the <p> paragraph style applied, But the problem I'm have with this is the if I have a <br/> break in the wysiwyg to start a new sentence directly below the previous sentence. The <br/> in the wysiwyg is lost and there's no space between the ending sentence's period and where the new sentence begins. Example (... this sentence has ended.This sentence has no space before previous sentence period.)

Code Option Two:
<?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;
}
?>


Has anyone else had this problem using maxWords and does anyone know of a fix to get this working?
Thanks, Zick
Attachments:

maxword-example.jpg 117K

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