Limit amount of text shown

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

I beleive I might have ask about this before but I noticed there are php codes to limit the amount of text that is shown with out having to set it in the cms builder itself. The only problem I have is it cuts the words off. Here is the code, is there a way to change it to show the full word and not cut it off thanks.

<?php echo substr($record['content'], 0, 150);?>
Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - October 30, 2008

Hi Glenn,

Often if you look in the comments on php.net for a function people will post other solutions for things like this.

Check out http://www.php.net/substr (just replace substr to look up any other function)

They have this one:

<?php

function cutText($string, $setlength) {
$length = $setlength;
if($length<strlen($string)){
while (($string{$length} != " ") AND ($length > 0)) {
$length--;
}
if ($length == 0) return substr($string, 0, $setlength);
else return substr($string, 0, $length);
}else return $string;
}

?>


Which you could use like this:

<?php echo cutText($record['content'], 0, 150); ?>

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

Re: [Dave] Limit amount of text shown

Thanks Dave. Where do I place the php function code.

<?php

function cutText($string, $setlength) {
$length = $setlength;
if($length<strlen($string)){
while (($string{$length} != " ") AND ($length > 0)) {
$length--;
}
if ($length == 0) return substr($string, 0, $setlength);
else return substr($string, 0, $length);
}else return $string;
}

?>

Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - October 30, 2008

You can put it at the top of the page, or anywhere before you call cutText().
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Limit amount of text shown

Dave it doesn't show any text. Do I need to change any of the "0" in the code?
Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - October 31, 2008

Oh, yes you're right. Drop the 0, try this:

<?php echo cutText($record['content'], 150); ?>
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Limit amount of text shown

Dave it works great thanks so much. One other question. If the content is shorter then the 150 limit it doesn't show anything. In that php function code is there a way to have it still show the content if it doesn;t meet the limit? Thanks again for your 100% customer support.
Glenn Rauchut (Owner) Emergency Designz

Re: [grauchut] Limit amount of text shown

By Dave - November 3, 2008

I'm not sure because I didn't write that function. You could check the comments or for other functions at the php.net link above.

Also, here's a function I wrote for Donna that limits by words:

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


Maybe that one will work better?
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Limit amount of text shown

Dave will that code still show the text if it doesxn't reach the limit. Say the limit is 150 and the client only enters 100. Thanks
Glenn Rauchut (Owner) Emergency Designz